How to implement proper HTTP error handling in .NET MVC 2?

Here’s one technique you could use. Define an ErrorsController which will serve the error pages: public class ErrorsController : Controller { public ActionResult Http404() { Response.StatusCode = 404; return Content(“404”, “text/plain”); } public ActionResult Http500() { Response.StatusCode = 500; return Content(“500”, “text/plain”); } public ActionResult Http403() { Response.StatusCode = 403; return Content(“403”, “text/plain”); } } … Read more

ASP.NET MVC 2.0 JsonRequestBehavior Global Setting

This, like other MVC-specific settings, is not settable via Web.config. But you have two options: Override the Controller.Json(object, string, Encoding) overload to call Json(object, string, Encoding, JsonRequestBehavior), passing JsonRequestBehavior.AllowGet as the last argument. If you want this to apply to all controllers, then do this inside an abstract base controller class, then have all your … Read more

ActionLink htmlAttributes with hyphens

The problem is that your anonymous object property data-icon has an invalid name. C# properties cannot have dashes in their names. There are two ways you can get around that: Use an underscore instead of dash (MVC will automatically replace the underscore with a dash in the emitted HTML): @Html.ActionLink(“Edit”, “edit”, “markets”, new { id … Read more

Session state can only be used when enableSessionState is set to true either in a configuration

Did you enable the session state in the section as well? <system.web> <pages enableSessionState=”true” /> </system.web> Or did you add this to the page? <%@Page enableSessionState=”true”> And did you verify that the ASP.NET Session State Manager Service service is running? In your screenshot it isn’t. It’s set to start-up mode Manual which requires you to … Read more

ASP.NET MVC Redirect with model

Straight from my own app: public ActionResult Create(Booking item) { if (ModelState.IsValid) { int newID = _tasks.Create(item); // NEW section to emulate model being populated for use in Details view TempData[“additionalData”] = “Person created successfully”; return RedirectToAction(“Details”, new { id = newID }); } else { return View(); } } then, couldn’t your “Details” action … Read more

Data Annotations for validation, at least one required field?

I have extended Zhaph answer to support grouping of properties. [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public class AtLeastOnePropertyAttribute : ValidationAttribute { private string[] PropertyList { get; set; } public AtLeastOnePropertyAttribute(params string[] propertyList) { this.PropertyList = propertyList; } //See http://stackoverflow.com/a/1365669 public override object TypeId { get { return this; } } public override bool IsValid(object value) { … Read more

The type arguments cannot be inferred from the usage. Try specifying the type arguments explicitly

I know this question already has an accepted answer, but for me, a .NET beginner, there was a simple solution to what I was doing wrong and I thought I’d share. I had been doing this: @Html.HiddenFor(Model.Foo.Bar.ID) What worked for me was changing to this: @Html.HiddenFor(m => m.Foo.Bar.ID) (where “m” is an arbitrary string to … Read more

Html.ActionLink with a specified HTML id?

You were on the right track. I am not sure why it didn’t work for you as your code has a typo which would have produced a } expected error. The following is what you are looking for: <%= Html.ActionLink(“Test Link”, “SomeAction”, “SomeController”, null, new {id = “someID” }) %> Which produces teh following HTML: … Read more

Firefox 6 Infinite Page Refresh With Page With Hash Tags

Turns out, this is an issue with an old version of MicrosoftAjax.js (the one that comes installed with Asp.Net MVC 2). Open up the MicrosoftAjax.debug.js file and check the file version number. The top of this file will look like this if this is your problem: // Name: MicrosoftAjax.debug.js // Assembly: System.Web.Extensions // Version: 4.0.0.0 … Read more