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 Adding Server-Side Validations Using ModelState

vincent batteast
vincent batteast
51,094 Points

In IssuesController.cs complete the server-side validation rule that enforces if the issue severity is "Critical" that t

I also try "issues.email" and that didn't work.

IssuesController.cs
using System.Web.Mvc;
using IssueReporter.Data;
using IssueReporter.Models;

namespace IssueReporter.Controllers
{
    public class IssuesController : Controller
    {
        private IssuesRepository _issuesRepository;

        public IssuesController()
        {
            _issuesRepository = new IssuesRepository();
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Report()
        {
            var issue = new Issue();

            SetupDepartmentsSelectListItems();

            return View(issue);
        }

        [HttpPost]
        public ActionResult Report(Issue issue)
        {
            if (issue.Severity == Issue.SeverityLevel.Critical && string.IsNullOrEmpty(issue.Email))
            {
                // TODO Use the ModelState.AddModelError method to add an error message for the "Email" field.
                // Bummer! Value cannot be null. Parameter name: key
                ModelState.AddModelError(issue.Email, "because it's critical you need a Email address");
            }

            if (ModelState.IsValid)
            {
                _issuesRepository.AddIssue(issue);

                return RedirectToAction("Index");
            }

            SetupDepartmentsSelectListItems();

            return View(issue);
        }

        private void SetupDepartmentsSelectListItems()
        {
            ViewBag.DepartmentsSelectListItems = new SelectList(
                Data.Data.Departments, "Id", "Name");
        }
    }
}

1 Answer

Steven Parker
Steven Parker
231,269 Points

You deviated a bit from the instructions.

The challenge says, "Pass in the string "Email" for the key parameter value", but you gave it issue.Email instead.

Also it says to use "the string "The Email field is required for critical issues." for the errorMessage parameter.", but you gave it "because it's critical you need a Email address".