NerdDinner 的 AddModelErrors 如何工作?

发布于 2024-07-25 23:04:04 字数 931 浏览 4 评论 0原文

我正在学习 NerDinner 免费教程 http://nerddinnerbook.s3.amazonaws.com/Intro.htm

我必须在第 5 步中的某处,它说为了使代码更简洁,我们可以创建一个扩展方法。 我查看了完整的代码,它具有使用扩展方法的功能:

catch
{
    ModelState.AddModelErrors(dinner.GetRuleViolations());
    return View(new DinnerFormViewModel(dinner));
}

然后将其作为扩展方法的定义。

namespace NerdDinner.Helpers {

    public static class ModelStateHelpers {

        public static void AddModelErrors(this ModelStateDictionary modelState, IEnumerable<RuleViolation> errors) {

            foreach (RuleViolation issue in errors) {
                modelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
            }
        }
    }
}

我尝试遵循教程中的内容并结合代码包含的内容,但收到了预期的错误,即不存在仅接受 1 个参数的 AddModelErrors 方法。

我显然在这里遗漏了一些非常重要的东西。 它是什么?

I'm going through the NerDinner free tutorial
http://nerddinnerbook.s3.amazonaws.com/Intro.htm

I got to somewhere in Step 5 where it says to make the code cleaner we can create an extension method. I look at the completed code and it has this to use the extension method:

catch
{
    ModelState.AddModelErrors(dinner.GetRuleViolations());
    return View(new DinnerFormViewModel(dinner));
}

And then this as the extension method's definition.

namespace NerdDinner.Helpers {

    public static class ModelStateHelpers {

        public static void AddModelErrors(this ModelStateDictionary modelState, IEnumerable<RuleViolation> errors) {

            foreach (RuleViolation issue in errors) {
                modelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
            }
        }
    }
}

I try to follow what the tutorial says combined with what the code contains but receive the expected error that there is no AddModelErrors method that accepts only 1 argument.

I'm obviously missing something very important here. What is it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

烏雲後面有陽光 2024-08-01 23:04:04

您需要包含助手参考;

using NerdDinner.Helpers;

然后

using NerdDinner.Models;

检查是否有效并添加错误;

if (!dinner.IsValid)
{
    ModelState.AddModelErrors(dinner.GetRuleViolations());
    return View(dinner);
}

您的晚餐还必须有部分课程;

public partial class Dinner
{
    public bool IsValid
    {
        get { return (GetRuleViolations().Count() == 0); }
    }

    public IEnumerable<RuleViolation> GetRuleViolations()
    {
        if (String.IsNullOrEmpty( SomeField ))
            yield return new RuleViolation("Field value text is required", "SomeField");
    }

    partial void OnValidate(ChangeAction action)
    {
        if (!IsValid)
            throw new ApplicationException("Rule violations prevent saving");
    }
}

不要忘记 RuleViolation 类;

public class RuleViolation
{
    public string ErrorMessage { get; private set; }
    public string PropertyName { get; private set; }

    public RuleViolation(string errorMessage)
    {
        ErrorMessage = errorMessage;
    }

    public RuleViolation(string errorMessage, string propertyName)
    {
        ErrorMessage = errorMessage;
        PropertyName = propertyName;
    }
}

You need to include the helpers reference;

using NerdDinner.Helpers;

and

using NerdDinner.Models;

Then check for valid and add the errors;

if (!dinner.IsValid)
{
    ModelState.AddModelErrors(dinner.GetRuleViolations());
    return View(dinner);
}

You must also have a partial class for your dinner;

public partial class Dinner
{
    public bool IsValid
    {
        get { return (GetRuleViolations().Count() == 0); }
    }

    public IEnumerable<RuleViolation> GetRuleViolations()
    {
        if (String.IsNullOrEmpty( SomeField ))
            yield return new RuleViolation("Field value text is required", "SomeField");
    }

    partial void OnValidate(ChangeAction action)
    {
        if (!IsValid)
            throw new ApplicationException("Rule violations prevent saving");
    }
}

Don't forget the RuleViolation class;

public class RuleViolation
{
    public string ErrorMessage { get; private set; }
    public string PropertyName { get; private set; }

    public RuleViolation(string errorMessage)
    {
        ErrorMessage = errorMessage;
    }

    public RuleViolation(string errorMessage, string propertyName)
    {
        ErrorMessage = errorMessage;
        PropertyName = propertyName;
    }
}
凉城凉梦凉人心 2024-08-01 23:04:04

如果您收到与此海报相同的错误消息:

“‘System.Web.Mvc.ModelStateDictionary’不包含‘AddModelErrors’的定义,并且没有扩展方法‘AddModelErrors’接受类型为‘System.Web.Mvc’的第一个参数可以找到 .ModelStateDictionary'(您是否缺少 using 指令或程序集引用?)”

您可能遇到此问题:

http://p2p .wrox.com/book-professional-asp-net-mvc-1-0-isbn-978-0-470-38461-9/74321-addmodalerrors-allcountries-page-87-view-data-dictionary.html#post248356

If you are receiving the same error message as this poster:

"'System.Web.Mvc.ModelStateDictionary' does not contain a definition for 'AddModelErrors' and no extension method 'AddModelErrors' accepting a first argument of type 'System.Web.Mvc.ModelStateDictionary' could be found (are you missing a using directive or an assembly reference?)"

You may be having this problem:

http://p2p.wrox.com/book-professional-asp-net-mvc-1-0-isbn-978-0-470-38461-9/74321-addmodalerrors-allcountries-page-87-view-data-dictionary.html#post248356

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