是什么状态。在ASP中有效。净MVC NerdDinner吗?

时间:2022-05-02 10:38:44

On the NerdDinner example of Professional ASP.NET MVC 1.0 there's a method to create a new dinner as copied bellow (page 89 of the free NerdDinner version).

浅谈专业ASP的内生实例。有一种方法可以复制bellow创建新的dinner(免费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?

它检查状态。是否是可用的事实。它似乎要检查模型是否对数据库有效(也就是说,它捕获数据类型转换,比如格式无效的日期,但不捕获业务规则)。这是真的吗?

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将是错误的,您将返回一个错误,但是仅仅是为了日期,因为addrule违规从未执行。如果您删除了对ModelState的检查。完全有效,然后您将得到所有错误(由于异常),包括日期中无效的标记。那么,为什么要检查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); 
} 

3 个解决方案

#1


127  

ModelState.IsValid tells you if any model errors have been added to 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 DataAnnotations model binder will fill model state with validation errors taken from the DataAnnotations attributes on your model.

示例dataannotation模型绑定器将使用从模型上的dataannotation属性中获取的验证错误来填充模型状态。

#2


25  

From the Errata: ModelState.AddRuleViolations(dinner.GetRuleViolations());

从勘误表:ModelState.AddRuleViolations(dinner.GetRuleViolations());

Should be:

应该是:

ModelState.AddModelErrors(dinner.GetRuleViolations());

ModelState.AddModelErrors(dinner.GetRuleViolations());

Reference: http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-MVC-1-0.productCd-0470384611,descCd-ERRATA.html

参考:http://www.wrox.com/wileycda/wroxtitle/professional - asp -网- mvc - 1 - 0. - productcd - 0470384611,descCd-ERRATA.html

#3


0  

Yes , Jared and Kelly Orr are right. I use the following code like in edit exception.

是的,Jared和Kelly Orr是对的。我使用如下代码,如编辑异常。

foreach (var issue in dinner.GetRuleViolations())
{
    ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
}

in stead of

代替

ModelState.AddRuleViolations(dinner.GetRuleViolations());

#1


127  

ModelState.IsValid tells you if any model errors have been added to 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 DataAnnotations model binder will fill model state with validation errors taken from the DataAnnotations attributes on your model.

示例dataannotation模型绑定器将使用从模型上的dataannotation属性中获取的验证错误来填充模型状态。

#2


25  

From the Errata: ModelState.AddRuleViolations(dinner.GetRuleViolations());

从勘误表:ModelState.AddRuleViolations(dinner.GetRuleViolations());

Should be:

应该是:

ModelState.AddModelErrors(dinner.GetRuleViolations());

ModelState.AddModelErrors(dinner.GetRuleViolations());

Reference: http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-MVC-1-0.productCd-0470384611,descCd-ERRATA.html

参考:http://www.wrox.com/wileycda/wroxtitle/professional - asp -网- mvc - 1 - 0. - productcd - 0470384611,descCd-ERRATA.html

#3


0  

Yes , Jared and Kelly Orr are right. I use the following code like in edit exception.

是的,Jared和Kelly Orr是对的。我使用如下代码,如编辑异常。

foreach (var issue in dinner.GetRuleViolations())
{
    ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
}

in stead of

代替

ModelState.AddRuleViolations(dinner.GetRuleViolations());