Friday, September 16, 2016

Understanding options for Html.ValidationSummary in ASP.NET MVC Views

If you are confused about how the @Html.ValidationSummary Razor Html Helper works, you are certainly not the only one!

You can read about the various options for the ValidationSummary control here: https://msdn.microsoft.com/en-us/library/ee839469%28v=vs.118%29.aspx

There are 3 MAJOR uses of @Html.ValidationSummary as follows:

  • @Html.ValidationSummary()
  • @Html.ValidationSummary(true)
  • @Html.ValidationSummary(false)

@Html.ValidationSummary()

No parameters for the ValidationSummary control is the equivalent of passing false as a parameter.  That is, the ValidationSummary control will display ALL Model errors including ModelState-level errors.



@Html.ValidationSummary(true)

For the true parameter, you can see the definition directly from Visual Studio Intellisense:


This causes the ValidationSummary to ONLY display Model errors and exclude any ModelState-level errors.  Therefore, if you use some code in your MVC Controller such as ModelState.AddModelError("", "some error"); will not be added to the ValidationSummary control for display in your Razor view.


@Html.ValidationSummary(false)

As with the no parameter version of the ValidationSummary Razor HTML Helper, the ValidationSummary control will display ALL Model errors including ModelState-level errors.

That is all there is to understanding how to use the ValidationSummary Html Helper in your ASP.NET MVC Web Applications!!




No comments:

Post a Comment