Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove required attribute in MVC4, although that has been made required in model

I am not able to do the same thing with string properties. In the below code, I want to remove the validation of the 'Last Name' and make it optional.

[Required(ErrorMessage = "Required")]    
[Display(Name="Last Name")]
public string LastName { get; set; }
like image 716
Subrata Chakraborty Avatar asked Dec 05 '22 06:12

Subrata Chakraborty


2 Answers

You can add following java-script to your view and it will to remove the required rule for LastName although it has been made required in model.

$("#LastName").rules("remove", "required");

Thanks!

like image 155
Saranga Avatar answered Jan 12 '23 19:01

Saranga


If you want to ModelState to be valid then try this:

   ModelState["LastName"].Errors.Clear();
if (ModelState.IsValid)
{
   // your logic
}

if you want to disable clientside validations then :

  @Html.EnableClientValidation(false);

Note: this will disable all the client side validations.

like image 21
Nikitesh Avatar answered Jan 12 '23 21:01

Nikitesh