How to validate one field related to another’s value in ASP .NET MVC 3

One possibility is to write a custom validation attribute: public class RequiredIfOtherFieldIsNullAttribute : ValidationAttribute, IClientValidatable { private readonly string _otherProperty; public RequiredIfOtherFieldIsNullAttribute(string otherProperty) { _otherProperty = otherProperty; } protected override ValidationResult IsValid(object value, ValidationContext validationContext) { var property = validationContext.ObjectType.GetProperty(_otherProperty); if (property == null) { return new ValidationResult(string.Format( CultureInfo.CurrentCulture, “Unknown property {0}”, new[] { _otherProperty … Read more

DropdownListFor default value

So, I did something like this: @Html.DropDownListFor(model => model.Dessert, new SelectList(Model.AvailableDesserts, “DessertID”, “DessertName”), “—Select A Dessert —“) Seems to work pretty well. Dessert in my viewmodel is the one selected by the user. AvailableDesserts is a collection of ones to pick from.

MVC 3 – Html.EditorFor seems to cache old values after $.ajax call

There is no caching involved here. It’s just how HTML helper work. They first look at the ModelState when binding their values and then in the model. So if you intend to modify any of the POSTed values inside your controller action make sure you remove them from the model state first: [HttpPost] public virtual … Read more

Application_Error not firing when customerrors = “On”

UPDATE Since this answer does provide a solution, I will not edit it, but I have found a much cleaner way of solving this problem. See my other answer for details… Original Answer: I figured out why the Application_Error() method is not being invoked… Global.asax.cs public class MvcApplication : System.Web.HttpApplication { public static void RegisterGlobalFilters(GlobalFilterCollection … Read more