书呆子晚餐没有发现错误
我正在阅读《Professional ASP.NET MVC 1.0》一书第 1 章中的“处理编辑错误”(第 67 页),但遇到了问题。
问题是,当我编辑晚餐并单击“保存”时,即使我将标题留空,它也不会捕获任何表单错误。 UpdateModel(dinner) 或dinnerRepository.Save() 都不会抛出错误。
当我保存后检查数据库时,标题字段确实为空。如何才能在不引发错误的情况下发生这种情况?
任何帮助将不胜感激。编辑下面的代码:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection formValues)
{
Dinner dinner = dinnerRepository.GetDinner(id);
try
{
UpdateModel(dinner);
dinnerRepository.Save();
return RedirectToAction("Details", new { id = dinner.DinnerID });
}
catch
{
foreach (var issue in dinner.GetRuleViolations())
{
ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
}
return View(dinner);
}
}
发现问题 - 事实上,我的部分是一个 N00B 错误。由于一些奇怪的原因,我注释掉了以下代码:
partial void OnValidate(ChangeAction action)
{
if (!IsValid)
throw new ApplicationException("Rule violations prevent saving");
}
难怪它没有捕获错误...... 感谢 RememberMe 尝试提供帮助!我很感激。
I'm at the "Handling Edit Errors" (page 67) in chapter 1 of the Professional ASP.NET MVC 1.0 book and I'm running into a problem.
The problem is that when I'm editing a dinner and I click save, it's not catching any of the form errors, even though I left the Title blank. Neither UpdateModel(dinner) or dinnerRepository.Save() throws an error.
When I check the db after the save, the Title field is indeed empty. How can that happen without throwing an error?
Any help would be appreciated. Edit code below:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection formValues)
{
Dinner dinner = dinnerRepository.GetDinner(id);
try
{
UpdateModel(dinner);
dinnerRepository.Save();
return RedirectToAction("Details", new { id = dinner.DinnerID });
}
catch
{
foreach (var issue in dinner.GetRuleViolations())
{
ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
}
return View(dinner);
}
}
Found the problem - It was, in fact, a N00B error on my part. I had the following code commented out for some strange reason:
partial void OnValidate(ChangeAction action)
{
if (!IsValid)
throw new ApplicationException("Rule violations prevent saving");
}
No wonder it wasn't catching the errors...
Thanks to RememberMe for trying to help out! I appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的模型中,您是否检查了该条件?
编辑:
看起来违规行为从未被添加,只是被计数。试试这个:
In your model, do you have a check for that condition?
EDIT:
Looks like the violations were never being added, just counted. Try this:
终于找到这本书的Wrox论坛有答案了。答案其实是令人惊讶的。它与 Visual Studio 2010 中的调试器有关。
基本上只需按 F5 即可继续,一切正常。
以下是包含更多答案的论坛帖子的链接:
http://p2p.wrox.com/book-professional-asp-net-mvc-2/79788-constraintexception-unhandled-user-code.html #post261814
Finally found the Wrox forum for this book that has the answer. The answer is actually surprising. It has something to do with the debugger in Visual Studio 2010.
Essentially just hit F5 to continue and everything works fine.
Here is the link to the forum thread with more answers:
http://p2p.wrox.com/book-professional-asp-net-mvc-2/79788-constraintexception-unhandled-user-code.html#post261814