C# ASP.NET MVC Forms Creating a Basic Form Invalid Values and ModelState

kevin hudson
PRO
kevin hudson
Pro Student 9,652 Points

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,804 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!

kevin hudson
PRO
kevin hudson
Pro Student 9,652 Points

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