如何找到异常/错误时TryUpdateModel未能更新asp.net MVC 3模型 [英] How to find the exceptions / errors when TryUpdateModel fails to update model in asp.net mvc 3

查看:343
本文介绍了如何找到异常/错误时TryUpdateModel未能更新asp.net MVC 3模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (!TryUpdateModel<Event>(evt))
{ 
   // ... I need to retrieve the errors here
}

有时, TryUpdateModel 未能更新模式。我无法找到原因和异常?

Sometimes, TryUpdateModel fails to update model. I am not able to find reason and exception?

推荐答案

根据其他 TryXXX 模式的方法(例如的TryParse )的<一个href=\"http://msdn.microsoft.com/en-us/library/dd460189%28v=vs.118%29.aspx\"><$c$c>TryUpdateModel方法返回一个布尔值,指示模式是否成功与否更新。

As per the other TryXXX paradigm methods (e.g. TryParse), the TryUpdateModel method returns a bool indicating whether the model was updated successfully or not.

TryUpdateModel 更新有错误的列表的ModelState 字典。如果 TryUpdateModel 失败(按照布尔回报),您可以遍历这些如下

TryUpdateModel updates the ModelState dictionary with a list of errors. If TryUpdateModel fails (as per the bool return), you can iterate these as follows:

 var model = new ViewModel();
 var isSuccess = TryUpdateModel(model);

 if (!isSuccess)
 {
     foreach (var modelState in ModelState.Values)
     {
        foreach (var error in modelState.Errors)
        {
           Debug.WriteLine(error.ErrorMessage);
        }
     }
 }

否则,如果你想有一个坚硬异常,那么使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.updatemodel.aspx\"><$c$c>UpdateModel来代替。

这篇关于如何找到异常/错误时TryUpdateModel未能更新asp.net MVC 3模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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