MVC模型未成功更新,但无法找到原因 [英] MVC Models not successfully updated but can't find reason

查看:138
本文介绍了MVC模型未成功更新,但无法找到原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这已经推动我疯了。

我不断收到以下异常

System.InvalidOperationException:类型Models.Expense'的模式没有成功更新。
   在System.Web.Mvc.Controller.UpdateModelTModel
   在System.Web.Mvc.Controller.UpdateModelTModel
   atMVC.Controllers.BaseExpenseController.Edit(字符型,收集的FormCollection)在C:\\项目\\ Expenses.MVC \\ \\控制器BaseExpenseController.cs:线​​109

System.InvalidOperationException: The model of type 'Models.Expense' was not successfully updated. at System.Web.Mvc.Controller.UpdateModelTModel at System.Web.Mvc.Controller.UpdateModelTModel atMVC.Controllers.BaseExpenseController.Edit(String id, FormCollection collection) in C:\Projects\Expenses.MVC\Controllers\BaseExpenseController.cs:line 109

但我不能追查为什么不更新,没有什么异常说明为什么它没有更新。

But I can't track down why it is not updating, nothing in the exception suggests why it has not updated.

任何指针?

推荐答案

捕获异常,或致电TryUpdateModel来代替。 TryUpdateModel不会抛出一个异常,如果它不能更新你的模型,它只是返回false。你会发现在的ModelState错误的详细信息克雷格的建议。
事实上的UpdateModel只是调用TryUpdateModel并抛出如果返回false。

Catch the exception or call TryUpdateModel instead. TryUpdateModel won't throw an exception if it can't update your model, it will just return false. You'll find the error details in the ModelState as suggested by Craig. In fact UpdateModel just calls TryUpdateModel and throws if it returns false.

这篇关于MVC模型未成功更新,但无法找到原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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