I have a viewmodel with two other models in it. Both have fields that are required(done with entity framework).
public class featureModel
{
public FEATURE FEATURE { get; set; }
public REQUIREMENTS REQUIREMENTS { get; set; }
}
On my page I have a dropdownlist, which is populated like this
ViewBag.FEATURE_ID = new SelectList(db.FEATURE, "FEATURE_ID", "Name_");
on view
@Html.DropDownList("FEATURE_ID", "ADD FEATURE")
Which will be a dropdownlist of all the features found, and on the top a ADD FEATURE option(when this is selected, i have some js to show the fields the user should input). IF this option is selected, then the user would need to input certain fields, if not they should not(and the fields are hidden). When my page validates, it requires that this needs to be populated, which makes sense in terms of validation.
Is there a way that I can set a condition to ignore this validation if the dropdownlist is on a certain option?
You implement
System.ComponentModel.DataAnnotations.IValidatableObjecton your ViewModel and perform validation there:Have a hidden field on your view that provides a value for
FeaturesRequired(you could do this with javascript soonchangeof your drop down) which can then be checked on model validation.IValidatable with unobtrusive ajax (client-side validation)
Have a look at this resource for validating
IValidatableObjecton client-side.