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

Subrata Chakraborty picture Subrata Chakraborty · Jul 16, 2014 · Viewed 16.4k times · Source

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; }

Answer

Saranga picture Saranga · Jul 16, 2014

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!