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 Creating a Basic Form Invalid Values and ModelState

Need clarification

when adding the ModelState the instructor uses first capitalization that matches the ViewBag values but the ModelState is lowercase as the same as the method parameters. Why does this work both ways? My code I put in lowercase with the exception of the date argument

  public ActionResult Add(DateTime? date, int? activityId, double? duration, 
                                Entry.IntensityLevel? intensity, bool? exclude, string notes)
        {


            ViewBag.Date = ModelState["Date"].Value.AttemptedValue;
            ViewBag.ActivityId = ModelState["activityId"].Value.AttemptedValue;
            ViewBag.Duration = ModelState["duration"].Value.AttemptedValue;
            ViewBag.Intensity = ModelState["intensity"].Value.AttemptedValue;
            ViewBag.Exclude = ModelState["exclude"].Value.AttemptedValue;
            ViewBag.Notes = ModelState["notes"].Value.AttemptedValue;

            return View();
        }

2 Answers

Allan Clark
Allan Clark
10,810 Points

The matching for the string passed in is not case sensitive. You would be able to use ModelState["duRAtiON"].Value.AttemptedValue as long as the spelling is correct. I can't say as to why it is like this, maybe for performance, or reduce complexity, would have to ask Microsoft haha.

Hope this helps Happy Coding!

Thanks Allen. It would be better to mention this in the video at some point or keep the naming conventions the same.