Disabling validation in @ Html.TextBoxFor in .Net

I am using ASP.Net MVC 3. I have an object called Student that has the properties Id, Name, Age, RollNo. On the Student creation page, I used the validation framework. But in the advanced search page, I use all the properties, but I do not want to use a validation system, since users may not use all fields for search.

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

Please help me overcome this problem.

Relations Mole

+6
source share
5 answers

I agree with xixonia, I use a separate view model for searching, but to answer your question you have several options for the client side:

  • Go to data-val = "false" as an HTML attribute, for example: @Html.TextBoxFor(x => x.Age, new { data_val = "false" });
  • Use @Html.TextBox() instead
  • Manually create your text box using HTML using the same input name so that it binds to the model

If you are performing a check at the back end (you must be!), That is, checking ModelState.IsValid , then you will have to remove the checking properties from ModelState, for example: ModelState.Remove("Age"); .

+15
source

You cannot delete an attribute at runtime. You will need to have several view models for one object to create and another for search.

+2
source

It seems that the values โ€‹โ€‹of the Student class are limited (as it should be).

I would like to use a separate view model for the search, rather than using Student.

 public class Student { [ValidationAttributeOfSomeKind] int Age { get; set; } [ValidationAttributeOfSomeKind] string Name { get; set; } } public class StudentSearch { int? Age { get; set; } string Name { get; set; } } 
+2
source

I think you need to write your own text box helper. Unable to disable this.

+1
source

Add this code to your form:

 @Html.EnableClientValidation(false); 
+1
source

Source: https://habr.com/ru/post/904216/


All Articles