Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# ASP.NET MVC Forms Adding Form Validation Displaying Validation Messages

Calvin Secrest
Calvin Secrest
24,815 Points

Bummer! Did you remove any of provided code or markup?

In Report.cshtml add a validation summary section by rendering a call to the Html.ValidationSummary HTML helper method just inside of the form above the "Name" field.

I'm not sure if I did this code challenge correctly.

Report.cshtml
@model IssueReporter.Models.Issue
@using IssueReporter.Models

@{
    ViewBag.Title = "Report an Issue";
}

<h2>@ViewBag.Title</h2>

@Html.ValidationSummary(True)

@using (Html.BeginForm())
{
    <div>
        @Html.LabelFor(m => m.Name)
        @Html.TextBoxFor(m => m.Name)
        @Html.ValidationMessageFor(m=> m.Name)
    </div>

    <div>
        @Html.LabelFor(m => m.Email)
        @Html.TextBoxFor(m => m.Email)
    </div>

    <div>
        @Html.LabelFor(m => m.DepartmentId)
        @Html.DropDownListFor(m => m.DepartmentId, (SelectList)ViewBag.DepartmentsSelectListItems)
    </div>

    <div>
        @Html.LabelFor(m => m.Severity)
        @Html.RadioButtonFor(m => m.Severity,
          Issue.SeverityLevel.Minor) @Issue.SeverityLevel.Minor
        @Html.RadioButtonFor(m => m.Severity,
          Issue.SeverityLevel.Major) @Issue.SeverityLevel.Major
        @Html.RadioButtonFor(m => m.Severity,
          Issue.SeverityLevel.Critical) @Issue.SeverityLevel.Critical
    </div>

    <div>
        @Html.CheckBoxFor(m => m.Reproducible) @Html.DisplayNameFor(m => m.Reproducible)
    </div>

    <div>
        @Html.LabelFor(m => m.DescriptionOfProblem)
        @Html.TextAreaFor(m => m.DescriptionOfProblem)
    </div>

    <button type="submit">Save</button>
}

3 Answers

Steven Parker
Steven Parker
229,732 Points

:point_right: You can count on "Bummer!" to indicate that your answer is not correct. :smile:

Some contributing factors might be:

  • The challenge asked you to use Html.ValidationSummary but you added Html.ValidationMessageFor
  • The challenge asked you to place it "above the "Name" field" but you put it below the "Name" field.
  • The challenge also said to place it "just inside of the form" but you have it inside the first <div>
Calvin Secrest
Calvin Secrest
24,815 Points

Thanks for you response and assistance

Figured it out. Only ValidationSummary html helper method is needed the field level message is not needed.

@model IssueReporter.Models.Issue
@using IssueReporter.Models

@{
    ViewBag.Title = "Report an Issue";
}

<h2>@ViewBag.Title</h2>

@using (Html.BeginForm())
{
     @Html.ValidationSummary()
    <div>
        @Html.LabelFor(m => m.Name)
        @Html.TextBoxFor(m => m.Name)
    </div>

    <div>
        @Html.LabelFor(m => m.Email)
        @Html.TextBoxFor(m => m.Email)
    </div>

    <div>
        @Html.LabelFor(m => m.DepartmentId)
        @Html.DropDownListFor(m => m.DepartmentId, (SelectList)ViewBag.DepartmentsSelectListItems)
    </div>

    <div>
        @Html.LabelFor(m => m.Severity)
        @Html.RadioButtonFor(m => m.Severity,
          Issue.SeverityLevel.Minor) @Issue.SeverityLevel.Minor
        @Html.RadioButtonFor(m => m.Severity,
          Issue.SeverityLevel.Major) @Issue.SeverityLevel.Major
        @Html.RadioButtonFor(m => m.Severity,
          Issue.SeverityLevel.Critical) @Issue.SeverityLevel.Critical
    </div>

    <div>
        @Html.CheckBoxFor(m => m.Reproducible) @Html.DisplayNameFor(m => m.Reproducible)
    </div>

    <div>
        @Html.LabelFor(m => m.DescriptionOfProblem)
        @Html.TextAreaFor(m => m.DescriptionOfProblem)
    </div>

    <button type="submit">Save</button>
}

I have tried this it is not working. Any suggestions what I am missing?

@model IssueReporter.Models.Issue
@using IssueReporter.Models

@{
    ViewBag.Title = "Report an Issue";
}

<h2>@ViewBag.Title</h2>


@using (Html.BeginForm())
{
    @Html.ValidationSummary()

    <div>
        @Html.LabelFor(m => m.Name)
        @Html.ValidationMessageFor(m => m.Name)
        @Html.TextBoxFor(m => m.Name)
    </div>

    <div>
        @Html.LabelFor(m => m.Email)
        @Html.TextBoxFor(m => m.Email)
    </div>

    <div>
        @Html.LabelFor(m => m.DepartmentId)
        @Html.DropDownListFor(m => m.DepartmentId, (SelectList)ViewBag.DepartmentsSelectListItems)
    </div>

    <div>
        @Html.LabelFor(m => m.Severity)
        @Html.RadioButtonFor(m => m.Severity,
          Issue.SeverityLevel.Minor) @Issue.SeverityLevel.Minor
        @Html.RadioButtonFor(m => m.Severity,
          Issue.SeverityLevel.Major) @Issue.SeverityLevel.Major
        @Html.RadioButtonFor(m => m.Severity,
          Issue.SeverityLevel.Critical) @Issue.SeverityLevel.Critical
    </div>

    <div>
        @Html.CheckBoxFor(m => m.Reproducible) @Html.DisplayNameFor(m => m.Reproducible)
    </div>

    <div>
        @Html.LabelFor(m => m.DescriptionOfProblem)
        @Html.TextAreaFor(m => m.DescriptionOfProblem)
    </div>

    <button type="submit">Save</button>
}