Can we display all errors in one go?

Answer

Yes, we can; use the ValidationSummary method from the Html helper class.

<%= Html.ValidationSummary() %>   

What are the other data annotation attributes for validation in MVC?

If you want to check string length, you can use StringLength.

[StringLength(160)]
public string FirstName { get; set; }

In case you want to use a regular expression, you can use the RegularExpression attribute.

[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}")]public string Email { get; set; }

If you want to check whether the numbers are in range, you can use the Range attribute.

[Range(10,25)]public int Age { get; set; }

Sometimes you would like to compare the value of one field with another field, we can use the Compare attribute.

public string Password { get; set; }[Compare("Password")]public string ConfirmPass { get; set; }

In case you want to get a particular error message , you can use the Errors collection.

var ErrMessage = ModelState["Email"].Errors[0].ErrorMessage;

If you have created the model object yourself you can explicitly call TryUpdateModel in your controller to check if the object is valid or not.

TryUpdateModel(NewCustomer);

In case you want add errors in the controller you can use the AddModelError function.

ModelState.AddModelError("FirstName", "This is my server-side error.");

All asp.net-mvc Questions

Ask your interview questions on asp-net-mvc

Write Your comment or Questions if you want the answers on asp-net-mvc from asp-net-mvc Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---