什么是ASP.NET MVC中的NerdDinner ModelState.IsValid有效期? [英] What is ModelState.IsValid valid for in ASP.NET MVC in NerdDinner?

查看:121
本文介绍了什么是ASP.NET MVC中的NerdDinner ModelState.IsValid有效期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在的<一的的NerdDinner 例子href=\"http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-MVC-1-0.productCd-0470384611.html\">Professional ASP.NET MVC 1.0 有一个方法来创建一个新的晚餐,波纹管(自由的NerdDinner版第89页)复印。

有它检查ModelState.IsValid真正的。它似乎检查模型是有效的数据库(也就是,它捕获的数据类型转换,就像格式无效日期,但不是业务规则)。是真的吗?

当提交表单,如果你有在日期错误,ModelState.IsValid将是假的,你会得到一个错误,但只适用于日期,因为从来没有执行AddRuleViolations。如果您删除了检查ModelState.IsValid完全,那么你会得到所有的错误(由于除外),包括日期标记时,它是无效的。那么,为什么是ModelState.IsValid检查那里呢?我缺少的东西吗?

  //
// POST:/晚餐/创建的[AcceptVerbs(HttpVerbs.Post)
公众的ActionResult创建(晚餐晚餐){
    如果(ModelState.IsValid){
        尝试{
            dinner.HostedBy =SomeUser的;            dinnerRepository.Add(晚餐);
            dinnerRepository.Save();            返回RedirectToAction(详细信息,新{ID = dinner.DinnerID});
        } {抓
            ModelState.AddRuleViolations(dinner.GetRuleViolations());
        }
    }
    返回查看(晚餐);
}


解决方案

ModelState.IsValid 如果有错误的模型已被添加到告诉你的ModelState

默认的模型粘合剂将增加对基本类型转换问题的一些错误(例如,通过为一些非数字这是一个INT)。您可以根据任何验证系统,您正在使用更充分地填充ModelState中。

样本 DataAnnotations 模型绑定将填补从模型中的 DataAnnotations 属性所验证错误模型状态。

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).

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?

// 
// 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); 
}

解决方案

ModelState.IsValid tells you if any model errors have been added to 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.

The sample DataAnnotations model binder will fill model state with validation errors taken from the DataAnnotations attributes on your model.

这篇关于什么是ASP.NET MVC中的NerdDinner ModelState.IsValid有效期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆