C# ASP.NET MVC更新模型没有';不行?

C# ASP.NET MVC更新模型没有';不行?,c#,.net,asp.net-mvc,updatemodel,sqlmetal,C#,.net,Asp.net Mvc,Updatemodel,Sqlmetal,我想更新一个客户端类型的实体 [HttpPost] public ActionResult Manage(String id, FormCollection collection) { // Create service ClientService service = new ClientService(); // Read existing client Client c = service.FindByUsername(id); // Update

我想更新一个客户端类型的实体

[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);            
}
服务返回客户端类型实体。客户端具有以下属性:用户名、名字、姓氏、Id-这些是提交的集合中的键

另外,客户端实体有一个订单列表(由SQLMetal添加)以及一个用于对象跟踪的版本字段

当UpdateModel行被命中时,它不会出错,但是对象c中的值不会被更新。问题不在于
service.Update(c)
,而在于
UpdateModel(c,“客户端”)

我做错了什么

多谢各位

编辑: 客户端由SQLMetal映射

其属性如下:

  • 整数Id
  • 字符串用户名
  • 字符串名
  • 字符串Lastname
  • 时间戳版本
  • 可撤销订单 错误(内部异常为空)

    用户代码未处理System.InvalidOperationException Message=无法更新“Shop.MVC.Data.Client”类型的模型。 Source=System.Web.Mvc 堆栈跟踪: 在System.Web.Mvc.Controller.UpdateModel[TModel](TModel模型,字符串前缀,字符串[]includeProperties,字符串[]excludeProperties,IValueProvider valueProvider) 在System.Web.Mvc.Controller.UpdateModel[TModel](TModel模型)中 在C:\Codebox\ARE02\VideoPlayerPrototype\Shop.MVC.Web\Controllers\ClientController.cs中的Shop.MVC.Web.ClientController.Manage(字符串id,FormCollection集合)中:第45行 在lambda_方法中(闭包、控制器基、对象[]) 位于System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller,Object[]参数) 位于System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext ControllerContext,IDictionary`2参数) 位于System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext ControllerContext,ActionDescriptor ActionDescriptor,IDictionary`2参数) 在System.Web.Mvc.ControllerActionInvoker.c_uuDisplayClass15.b_uuu12()中 位于System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter筛选器、ActionExecutingContext预文本、Func`1 continuation) 内部异常:
    您需要提交ClientService上的更改

    最有可能的问题是没有以
    “Client”
    开头的属性

    在不了解模型细节的情况下,很难说要删除
    “客户机”
    ,我相信这会解决问题


    更新
    您可能有一些验证规则。尝试使用
    TryUpdateModel()
    ,它不会对模型进行验证。

    我同意Sergey的观点,您需要调用Save changes才能持久化此更改。从您的post操作中,我看不到任何地方会将其持久化到DB中。您只是调用UpdateModel,但我看不到任何保存更改


    希望这有帮助

    问题出在HTML结构中-有一个嵌套表单导致UpdateModel方法失败,因为它包含两个表单的FormCollection。

    我得到“无法更新'Shop.MVC.Data.Client'类型的模型”异常。我无法调试它,因为它被Visual Studio跳过。我以前试过。你也需要分享你的模型。同时放入完整的错误堆栈。内部异常为null。1分钟@model。我需要看看这个类的代码。但是我可以从属性名称中看出,它将不会被更新,因为没有一个以“Client”开头。我已经删除了客户端,并且得到了我在第一条评论中写到的异常:“Shop.MVC.Data.Client类型的模型”无法更新。“TryUpdateModel()将在失败时吞下异常..您的HTML是什么样子的?您好,谢谢你的回复。我说过“当UpdateModel行被命中时,它不会出错,但是对象c中的值不会被更新。问题不在service.Update(c)中,而是在UpdateModel(c,“Client”)。”当调试器经过service.Update(c)行时,它应该更改c中的值,而实际上它不会。用于持久化对象的方法有效,并且已经过测试:(感谢您尝试帮助Hi,service.Update(c)更新模型。问题是在调用此方法之前未设置c中的值。谢谢
    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: