我想更新客户端类型实体。
[HttpPost]
public ActionResult Manage(String id, FormCollection collection)
{
// Create service
ClientService service = new ClientService();
// Read existing client
Client c = service.FindByUsername(id);
// Update client by using values from submitted form collection
UpdateModel(c, "Client");
service.Update(c);
return View(c);
}
服务返回客户端类型实体。客户端具有以下属性:Username、FirstName、LastName、Id - 这些是提交的集合中的键。
此外,客户端实体具有订单列表(由 SQL Metal 添加)以及用于对象跟踪的版本字段。
当 UpdateModel 行被命中时,它不会出错,但对象 c 中的值不会更新。问题不在 service.Update(c)
中,而是在 UpdateModel(c, "Client")
中。
我做错了什么?
谢谢
编辑: 客户端由 SQL Metal 映射。
其属性如下:
- 整数 ID
- 字符串用户名;
- 字符串名字;
- 字符串姓氏;
- 时间戳版本
- 可查询订单;
错误(内部异常为空)
System.InvalidOperationException was unhandled by user code
Message=The model of type 'Shop.MVC.Data.Client' could not be updated.
Source=System.Web.Mvc
StackTrace:
at System.Web.Mvc.Controller.UpdateModel[TModel](TModel model, String prefix, String[] includeProperties, String[] excludeProperties, IValueProvider valueProvider)
at System.Web.Mvc.Controller.UpdateModel[TModel](TModel model)
at Shop.MVC.Web.Controllers.ClientController.Manage(String id, FormCollection collection) in C:\Codebox\ARE002\VideoPlayerPrototype\Shop.MVC.Web\Controllers\ClientController.cs:line 45
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
InnerException:
最佳答案
最有可能的问题是没有任何属性以“Client”
开头。
在不知道模型的详细信息的情况下,很难说,但删除“Client”
,我相信这应该可以解决问题。
更新
您可能有一些验证规则。尝试使用 TryUpdateModel()
,它不会对模型进行验证。
关于c# - ASP.NET MVC 更新模型不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5702861/