C# ASP.NET MVC Forms Adding Form Validation Adding Server-Side Validations Using ModelState

What am I missing here?

Need help please

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))
      {      
           ModelState.AddModelError("Email","The Email field is required for critical issues");

            }
            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

Balazs Pukli
Balazs Pukli
46,069 Points

Of course, your code is perfect syntactically. It's just you left out the dot ( "." ) from the end of the error message, so the unit test for this task doesn't pass. But programmaticaly speaking you have done nothing wrong.

Oh, wow. Thank you.