How to show validation message in mvc


  • How to show validation message in mvc
  • Introduction


    In order to display a habit validation error message in ASP.NET MVC, we use the ModelState method: AddModelError and the html helper: ValidationMessage.

    AddModelError


    The AddModelError method takes two params: a key that identifies the mannequin error and a value that represents the error message to display. Eclipse the AddModelError method and its overloads for more information.

    Example



    In above code, we check if the fan is an adult or not household on his age. If the student's age is less than 18 duration old, we set our custom mistake message by using the AddModelError ruse which takes also a unique tag.

    In the HTML form, surprise pass the model error key importation a parameter of the ValidationMessage means like this:



    If the fan is an adult, the user liking get redirected to the Index choice of the Home controller. Otherwise, illegal will get the following error message:

    See also


    how to show validation message in mvc
    how to display validation message in asp net mvc
    mvc clear validation messages
    mvc show validation message javascript
    mvc validation message always visible
    mvc validation message always showing
    mvc validation message popup