Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling validation in @Html.TextBoxFor in .Net

I'm using ASP.Net MVC 3 . I've an entity called Student having the properties Id, Name, Age, RollNo. In the create page of Student, I've used validation framework. But in Advanced Search page I'm using all the properties but don't want to use validation framework as users may not want to use all the fields for searching.

I would also like to mention that I've used [Required] annotation in model class.

Please help me to overcome this issue.

Regards Molay

like image 680
Molay Avatar asked Dec 21 '11 05:12

Molay


3 Answers

I would agree with xixonia, use a separate View Model for search but to answer your question you have a few options client side:

  1. Pass in data-val = "false" as an HTML attribute, e.g: @Html.TextBoxFor(x => x.Age, new { data_val = "false" });
  2. Use @Html.TextBox() instead
  3. Manually create your text box with HTML using the same input name so it binds to the model

If you're doing validation in the back end (you should be!), i.e. checking ModelState.IsValid then you will have to remove the validation properties from the ModelState, like so: ModelState.Remove("Age");.

like image 132
eth0 Avatar answered Sep 17 '22 15:09

eth0


Add this code on your form:

@Html.EnableClientValidation(false);
like image 41
Morteza Avatar answered Sep 19 '22 15:09

Morteza


You can not remove attribute at run-time. You will have to have multiple view-models for student entity one for create and other for search.

like image 33
Maheep Avatar answered Sep 20 '22 15:09

Maheep