Using handleError out of the box in mvc3, problem - asp.net-mvc-3

I trying to do something like that
First, Create a new mvc 3 project in visual studio 2010
Next, Turning on the custom error in the Views\Shared\Web.config
<system.web>
<customErrors mode="On"/>
...
And then, I put the Tag in the Index ActionResult, Home Controller
Public Class HomeController
Inherits System.Web.Mvc.Controller
<HandleError()> _
Function Index() As ActionResult
ViewData("Message") = "Welcome to ASP.NET MVC!"
Throw New InvalidOperationException
Return View()
End Function
Function About() As ActionResult
Return View()
End Function
End Class
Finally run the app, and always show the yellow message error. I review a lot of examples and always indicated that is correct, but doesn't work.
I appreciate your help

You should do this in the main ~/web.config file, not the one in ~/Views/Shared/Web.config:
<system.web>
<customErrors mode="On"/>
...
</system.web>
Also ensure that ~/Views/Shared/Error.cshtml is present as this will be the rendered view in case of an exception.
And you no longer need to decorate your controller with the <HandleError()> attribute as ASP.NET MVC 3 uses a global filter for this.

Related

razor: ignore httprequestvalidationexception

I am using MVC3 Razor. I want my server to ignore the httprequestvalidation exception. I have tried
and all codes like [ValidateInput(false)]
none of these codes seem to work with MVC3 .net framework 4.0 razor
[ValidateInput(false)]
public ActionResult Index()
{
// ...method body
}
Using MVC 3, you must also ensure this is in your Web.config:
<system.web><httpRuntime requestValidationMode="2.0" /></system.web>

Validation Firing on Page Load

Currently, I have an MVC 3 app using the Razor View engine. I have unobtrusive validation enabled. The problem is that for some reason, on page load, my Edit View is displaying errors for required fields (even though the fields have a value). Has anyone else ran into this? Any suggestions for resolving this? Thanks.
Sample Field with problem:
<div class="full">
<label>Description:</label>
#Html.EditorFor(x=>x.Description, new{#class="super-textarea"})
#Html.ValidationMessageFor(x => x.Description)
</div>
Data Annotations on Model:
[Required, DataType(DataType.MultilineText)]
public virtual string Description { get; set; }
WebConfig enabled settings:
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
And of course the proper jquery files....
You can also clear the errors from the ModelState
ModelState.Clear();
Ok. Found the problem. Validation was happening due to Model binding attempting to take place. This was happening because our Get Method looks like this.
[HttpGet, RequestedObjectFilter]
public virtual ViewResult Edit(TKey id, T requestedObject)
{
return View(requestedObject);
}
A feature of .NET MVC is that anytime a reference value is passed as a parameter in the Method Signature of a ViewResult, ModelBinding is triggered, which in turn fires off validation. The reason that we were passing in the object to our method was due to our RequestedObjectFilter which would fetch the related entity from our abstracted repository, and pass it in to this method via the ActionParameters property. We refactored our RequestedObjectFilter to set the ViewModel instead, allowing us to remove the parameter from the method, thus solving the problem. Now our method looks like this:
[HttpGet, RequestedObjectFilter]
public virtual ViewResult Edit(TKey id)
{
return View();
}

Umbraco 5 - passing data from controller to view

I have been playing around with Umbraco 5 for some days now. I have made a partial view with some dummy-text that I have inserted into a page template. Work's fine. The problem is when I have to pass data from a controller to the view.
The view inherit from RenderViewPage (#inherits RenderViewPage) as default in Umbraco 5. I tried to do it the regular MVC way by #model ViewPage<Umbraco.Cms.Web.UI.Models.Test> but I got an error.
You should start by creating a Surface controller (can be done in seperate project or create controller folder directly in main project):
public class ContactFormSurfaceController : SurfaceController
{
[ChildActionOnly]
public PartialViewResult ContactForm()
{
var model = new ContactViewModel();
return PartialView(model);
}
}
Don't inherit form RenderViewPage just strongly type your view with your own model
Then create a macro that call the ChildAction ContactForm
You can add your action through editor or via code in templates: #Umbraco.RenderMacro("ContactForm")

ValidateRequest in Razor syntax

I have the following header of ASP.Net MVC page:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Admin.Master" Inherits="System.Web.Mvc.ViewPage<NEOGOV_Ideas.Models.SubIdeaAdminPage>"
ValidateRequest="false" %>
I need to move this page to Razor syntax. How should I set ValidateRequest?
Thanks
Decorate your action method with ValidateInput attribute
[HttpPost]
[ValidateInput(false)]
public ActionResult index()
{
return view();
}
You shouldn't need that line in the view, instead use the ValidateInput(false) attribute on the controller method.
Make sure you've got this in your web.config if you're using ASP .net 4.0 (which I presume you are if you're using MVC 3)
<httpRuntime requestValidationMode="2.0"/>
Martin
From MVC 4 we can allow html content only for property of model class, not for the whole request. Just need to mark property by attribute AllowHtml
public class EditorialPixlocateRequestViewModel
{
[AllowHtml]
public string Xml { get; set; }
}

strange behavior of MVC3

I created a custom Model i.e. to support my Razor View. Then I created a controller as following`namespace MyCandidate.Controllers
public class CandidateViewModelController : Controller
{
//
// GET: /CandidateViewModel/
public ActionResult Index()
{
return View();
}
}
I also have the following statement in my _Layout.cshtml
#Html.ActionLink("Canid", "Index", "CandidateViewModel")
Next i created a view and the very first statement of the view is
#model MyCandidate.Models.CandidateViewModel
when i run my project i get the following error
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
I spent more than 3 hours but could not figure out?
your Index() not get any parameters, but you send "CandidateViewModel") add Index(string input) method with attribute [HttpGet] to controller.
this error meen you haven't view "Index" in "Views/CandidateViewModel/Index.cshtml".
Maybe you delete master page files (_ViewSrat, _Layout)
Or you made a mistake when you change your routes
Change your ActionLink to:
#Html.ActionLink("Canid", "Index")
If you want to pass any data to View , you can also use ViewBag:
// Controller :
ViewBag.CandidateValues = CandidateViewModelData;
// View
#Html.Label("LabelName", (CandidateViewModel) ViewBag.CandidateValues.FiledName);

Resources