I have a view model like so:
public class AccountView {
public int AccountId { get; set; }
[Required]
public string AccountName { get; set; }
}
But I would like the AccountName field to be required only if the action is “Edit”, but not “Create”. How is this accomplished in asp.net mvc3?
thanks.
You can implement a custom validator. Once inside the Validate method you will have access to properties like, (((System.Web.Mvc.ModelValidator)(this))).ControllerContext.Controller where you can look into the ContollerContext, ViewData, etc. You also have access to properties like