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

ASP.NET MVC forms

In IssuesController.cs complete the server-side validation rule that enforces if the issue severity is "Critical" that the Issue.Email property is not null or empty. The conditional logic for this validation rule has already been added to the Report POST action method. Add a call to the ModelState.AddModelError method where the TODO comment is located. Pass in the string "Email" for the key parameter value and the string "The Email field is required for critical issues." for the errorMessage parameter.

I DONT UNDERSTAND THE QUESTION>>>PLEASE HELP ME

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))
            {
             if(ModelState.IsNullOrEmpty("Email") && issue.Email <=0)
             {
             ModelState.AddModelError("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");
        }
    }
}

2 Answers

Steven Parker
Steven Parker
231,198 Points

You're working to hard.

You apparently missed where the instructions said, "The conditional logic for this validation rule has already been added to the Report POST action method." So you don't need that if statement at all.

But when you call ModelState.AddModelError, remember that it takes two arguments. So be sure to provide both.

What was stated above was actually a very good hint in fact it worked for me. What this exercise is try to teach is how to target the field. At least that was the way I read it. I hope this helps anyone looking at it.

if (issue.Severity == Issue.SeverityLevel.Critical && string.IsNullOrEmpty(issue.Email)) <----provided code { // TODO Use the ModelState.AddModelError method to add an error message for the "Email" field. ModelState.AddModelError("Email", "The Email field is required for critical issues."); //"Email" is called from issue.Email but because it is a string you can target it //with "Email"
}