NerdDinner 的 AddModelErrors 如何工作?
我正在学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要包含助手参考;
然后
检查是否有效并添加错误;
您的晚餐还必须有部分课程;
不要忘记
RuleViolation
类;You need to include the helpers reference;
and
Then check for valid and add the errors;
You must also have a partial class for your dinner;
Don't forget the
RuleViolation
class;如果您收到与此海报相同的错误消息:
“‘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