I have a viewmodel where I have a field called Type be required:
[Required]
public string Type{ get; set; }
I am using this viewmodel on different pages but in some cases, the Type is not required.
I am wondering how I can tell a page to not have Type be Required.
What I tried was to put a
@Html.HiddenFor(model => model.Type)
but that did not work.
If you want to use the same view model for whatever reason have a look at using the foolproof library, foolproof aims to extend the Data Annotation validation provided in ASP.NET MVC. Initial efforts are focused on adding contingent validation.
The required attributes out of he box are:
The beauty of Foolproof is that it supports client side validation using unobstrusive techniques out of the box.
So in your case
The property type would only validate if ShouldValidateType is true.