Questions tagged «nerddinner»

4
NerdDinner的ASP.NET MVC中有效的ModelState.IsValid是什么?
在Professional ASP.NET MVC 1.0的NerdDinner示例中,有一种方法可以创建一个新的晚宴,如下所示(免费的NerdDinner版本的第89页)。 在那里检查ModelState.IsValid是否为true。似乎正在检查模型是否对数据库有效(也就是说,它会捕获数据类型转换,例如格式无效的日期,而不是业务规则)。真的吗? 提交表单时,如果日期中有错误,则ModelState.IsValid为false,并且会返回错误,但仅针对该日期,因为从未执行过AddRuleViolations。如果完全删除对ModelState.IsValid的检查,则将获得所有错误(由于异常),包括无效日期的标记。然后,为什么根本不检查ModelState.IsValid?我想念什么吗? // // POST: /Dinners/Create [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(Dinner dinner) { if (ModelState.IsValid) { try { dinner.HostedBy = "SomeUser"; dinnerRepository.Add(dinner); dinnerRepository.Save(); return RedirectToAction("Details", new {id = dinner.DinnerID }); } catch { ModelState.AddRuleViolations(dinner.GetRuleViolations()); } } return View(dinner); }
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.