Understanding Validation In MVC - Part 4

This article focuses on implementing validation summary in MVC and continuation of the previous article.

Understanding Validation in MVC - Part 3

Validation Summary

Validation Summary helps you to display all your validation error messages in the summary box.

It will display all your validation errors in unordered list format. excludePropertyErrors: true to have the summary display model-level errors only, or false.

We have the following steps to implement it.

Step 1. Define the validation summary inside your form as in the following.

Validation summary

Step 2. So my final View code will look like the following code snippet.

@model DropdownGrid.Models.StudentDetailsModel

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-1.10.2.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>

@Styles.Render("~/Content/css")

<script type="text/javascript">
    function foo(result) {
        $('#dvCategoryResults').html(result);
    }
</script>

<br>
<br>
<br>
<br>

<fieldset>
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(false)

        <table>
            <tr>
                <td>
                    @Html.LabelFor(M => M.SelectedStudent.Id)
                </td>
                <td>
                    @Html.TextBoxFor(M => M.SelectedStudent.Id)
                    @Html.ValidationMessageFor(M => M.SelectedStudent.Id,"*")
                </td>
            </tr>
            <tr>
                <td>
                    @Html.LabelFor(M => M.SelectedStudent.Name)
                </td>
                <td>
                    @Html.TextBoxFor(M => M.SelectedStudent.Name)
                    @Html.ValidationMessageFor(M => M.SelectedStudent.Name, "*")
                </td>
            </tr>
            <tr>
                <td>
                    @Html.LabelFor(M => M.SelectedStudent.Address)
                </td>
                <td>
                    @Html.TextBoxFor(M => M.SelectedStudent.Address)
                    @Html.ValidationMessageFor(M => M.SelectedStudent.Address, "*")
                </td>
            </tr>
            <tr>
                <td>
                    @Html.LabelFor(M => M.SelectedStudent.Class)
                </td>
                <td>
                    @Html.TextBoxFor(M => M.SelectedStudent.Class)
                    @Html.ValidationMessageFor(M => M.SelectedStudent.Class, "*")
                </td>
            </tr>
            <tr>
                <td>
                    @Html.LabelFor(M => M.SelectedStudent.Section)
                </td>
                <td>
                    @Html.TextBoxFor(M => M.SelectedStudent.Section)
                    @Html.ValidationMessageFor(M => M.SelectedStudent.Section, "*")
                </td>
            </tr>
            <tr>
                <td>
                    @Html.LabelFor(M => M.SelectedStudent.Email)
                </td>
                <td>
                    @Html.TextBoxFor(M => M.SelectedStudent.Email)
                    @Html.ValidationMessageFor(M => M.SelectedStudent.Email, "*")
                </td>
            </tr>
            <tr>
                <td>
                    @Html.LabelFor(M => M.SelectedStudent.Percentage)
                </td>
                <td>
                    @Html.TextBoxFor(M => M.SelectedStudent.Percentage)
                    @Html.ValidationMessageFor(M => M.SelectedStudent.Percentage, "*")
                </td>
            </tr>
            <tr>
                <td>
                    @Html.LabelFor(M => M.SelectedStudent.AdmissionDate)
                </td>
                <td>
                    @Html.TextBoxFor(M => M.SelectedStudent.AdmissionDate)
                    @Html.ValidationMessageFor(M => M.SelectedStudent.AdmissionDate, "*")
                </td>
            </tr>
        </table>
        <input type="submit" value="Submit" />
    }
</fieldset>

Step 3. Execute the project and you will find all the validations are visible in the validation summary box.

 Execute the project

Hope you understood the validation summary. I have explained some more things in Validation and advanced topics in the Validation summary.


Similar Articles