I’m new to MVC3. I have problem in custom validation, for example
In my BasicInfoViewModel.cs,
[Required]
[Display(Name = "State", ResourceType = typeof(Resources.Global))]
public string State { get; set; }
[Display(Name = "City", ResourceType = typeof(Resources.Global))]
public string City { get; set; }
In my BasicDetailsView.cshtml,
<label>
<span class="td">@Application.Resources.Global.State</span>
@Html.DropDownListFor(m => m.State, (List<SelectListItem>)ViewData["State"])
</label>
<label>
<span class="td">@Application.Resources.Global.City</span>
@Html.DropDownListFor(m => m.City, (List<SelectListItem>)ViewData["City"])
</label>
If the state property returns true, then only “City” is required. If not, City is not required, then the textbox should be diabled.
I’m not using EditorFor, using DropDownListFor because i’m using plain html. Can anyone help me to solve this issue?
Thanks…
MVC Foolproofis a set of validation data annotations that extend the existing ones and provide additional functionality. For example the[RequiredIfNotEmpty]attribute from this package is quite suitable for your scenario as it allows for conditional validation.Now the State property is optional. But if it has some value then the City property is required.