Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.