Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Part 86 - ValidationSummary in asp.net mvc

Suggested Videos 
Part 83 - RegularExpression attribute
Part 84 - Compare attribute
Part 85 - Enable client side validation



In this video, we will discuss displaying all validation errors at one place using validation summary html helper. A red asterisk (start) should be displayed next to every field that has failed the validation. The output should be as shown below.
validation summary in asp.net mvc



We will be working with the example that we discussed in Part 85. Please watch Part 85, before proceeding.

First of all, the validation error messages should be displayed in red color. On the "Edit" view, include a reference to Site.css style sheet.
<link href="~/Content/Site.css" rel="stylesheet" type="text/css" />

In site.css there are 3 CSS classes that control the display of validation error messages. Adjust the styles of these classes according to your needs.
field-validation-error
input.input-validation-error
validation-summary-errors

To display all errors at one place, use ValidationSummary() HTML helper. 
@Html.ValidationSummary(false, "Please fix the following errors and then submit the form")

To display an asterisk, next to the field that has failed validation, modfiy ValidationMessageFor() html helper as shown below.
@Html.ValidationMessageFor(model => model.Name, "*")

At this point, next to the field that has failed the validation message, a star symbol will be displayed instead of the detailed error message. All the detailed error messages will be displayed using validation summary.

8 comments:

  1. Plz Post Video for Ajax with Mvc

    ReplyDelete
  2. I have a problem with "jquery.validate.unobtrusive.min.js" When I click at the Save button I get the message: "The field HireDate must be a date. I think it have something to do with Globilization and different dateformat's. How can I solve this problem?

    ReplyDelete
    Replies
    1. You can create a new project to get the site.css file

      Delete
  3. https://www.youtube.com/watch?v=DhOvRZb8htU

    not difficult to find at all, that is good

    ReplyDelete
  4. sir from where i can get site.css file

    ReplyDelete
  5. please post video for validation( ccolor red change to green) using jquery in mvc

    ReplyDelete
  6. --field-validation-error
    field-validation-error {
    color: #ff0000;
    font-weight: bold;
    }

    -- input.input-validation-error
    .field-validation-error {
    display:none;
    }

    input.input-validation-error {
    border: 1px solid #ff0000;
    }

    -- validation-summary-errors
    .validation-summary-errors {
    color: #ff0000;
    font-weight: bold;
    font-size: 1.1em;
    }

    .validation-summary-valid {
    display: none;
    }

    ReplyDelete

It would be great if you can help share these free resources