asp.net-mvc 什么是 ModelState.IsValid 在 NerdDinner 的 ASP.NET MVC 中有效?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/881281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is ModelState.IsValid valid for in ASP.NET MVC in NerdDinner?
提问by pupeno
On the NerdDinnerexample of Professional ASP.NET MVC 1.0there's a method to create a new dinner as copied bellow (page 89 of the free NerdDinner version).
在Professional ASP.NET MVC 1.0的NerdDinner示例中,有一种创建新晚餐的方法,如下所示(免费 NerdDinner 版本的第 89 页)。
There it checks ModelState.IsValid for true. It seems to check if the model is valid for the database (that is, it catches data type conversions, like dates with invalid format, but not business rules). Is that true?
在那里它检查 ModelState.IsValid 是否为真。它似乎检查模型是否对数据库有效(即,它捕获数据类型转换,例如格式无效的日期,但不捕获业务规则)。真的吗?
When submitting the form, if you have an error in the date, ModelState.IsValid will be false and you'll get back an error, but only for the date because AddRuleViolations was never executed. If you remove the check for ModelState.IsValid completely, then you'll get all the errors (due to the exception), including a marking in the date when it is invalid. Then, why is the check for ModelState.IsValid there at all? Am I missing something?
提交表单时,如果您在日期中有错误,则 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 Brad Wilson
ModelState.IsValidtells you if any model errors have been added to ModelState.
ModelState.IsValid告诉您是否已将任何模型错误添加到ModelState.
The default model binder will add some errors for basic type conversion issues (for example, passing a non-number for something which is an "int"). You can populate ModelState more fully based on whatever validation system you're using.
默认模型绑定器将为基本类型转换问题添加一些错误(例如,为“int”类型传递非数字)。您可以根据您使用的任何验证系统更全面地填充 ModelState。
The sample DataAnnotationsmodel binder will fill model state with validation errors taken from the DataAnnotationsattributes on your model.
示例DataAnnotations模型绑定器将使用从DataAnnotations模型属性中获取的验证错误填充模型状态。
回答by Kelly Orr
From the Errata:
ModelState.AddRuleViolations(dinner.GetRuleViolations());
来自勘误表:
ModelState.AddRuleViolations(dinner.GetRuleViolations());
Should be:
应该:
ModelState.AddModelErrors(dinner.GetRuleViolations());
ModelState.AddModelErrors(dinner.GetRuleViolations());
参考:http: //www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-MVC-1-0.productCd-0470384611,descCd-ERRATA.html
回答by Phoe Htoo
Yes , Jared and Kelly Orr are right. I use the following code like in edit exception.
是的,贾里德和凯利奥尔是对的。我在编辑异常中使用以下代码。
foreach (var issue in dinner.GetRuleViolations())
{
ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
}
in stead of
代替
ModelState.AddRuleViolations(dinner.GetRuleViolations());
回答by B.Khan
All the model fields which have definite types, those should be validated when returned to Controller. If any of the model fields are not matching with their defined type, then ModelState.IsValid will return false. Because, These errors will be added in ModelState.
所有具有确定类型的模型字段,这些字段在返回到 Controller 时应进行验证。如果任何模型字段与其定义的类型不匹配,则 ModelState.IsValid 将返回 false。因为,这些错误会被添加到 ModelState 中。

