书呆子晚餐没有发现错误

发布于 2024-08-22 14:50:12 字数 1169 浏览 1 评论 0原文

我正在阅读《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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守护在此方 2024-08-29 14:50:12

在您的模型中,您是否检查了该条件?

public IEnumerable<RuleViolation> GetRuleViolations()
        {
            if (String.IsNullOrEmpty(title.Trim()))
                yield return new RuleViolation("Dinner Title is required", "title");

            yield break;
        }

编辑:
看起来违规行为从未被添加,只是被计数。试试这个:

if (ModelState.IsValid)
            {

                try
                {
                    ...
                }
                catch
                {
                    ModelState.AddModelErrors(dinner.GetRuleViolations());
                }
            }
            else
            {
                ModelState.AddModelErrors(dinner.GetRuleViolations());
            }

In your model, do you have a check for that condition?

public IEnumerable<RuleViolation> GetRuleViolations()
        {
            if (String.IsNullOrEmpty(title.Trim()))
                yield return new RuleViolation("Dinner Title is required", "title");

            yield break;
        }

EDIT:
Looks like the violations were never being added, just counted. Try this:

if (ModelState.IsValid)
            {

                try
                {
                    ...
                }
                catch
                {
                    ModelState.AddModelErrors(dinner.GetRuleViolations());
                }
            }
            else
            {
                ModelState.AddModelErrors(dinner.GetRuleViolations());
            }
青衫儰鉨ミ守葔 2024-08-29 14:50:12

终于找到这本书的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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文