简体   繁体   English

ASP.NET MVC更新模型不起作用?

[英]ASP.NET MVC Update Model Doesn't Work?

I'd like to update a Client type entity. 我想更新一个客户端类型的实体。

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

Service returns Client type entity. 服务返回客户端类型实体。 Client has the following properties: Username, FirstName, LastName, Id - these are the keys in submitted collection. 客户端具有以下属性:用户名,名字,姓氏,标识-这些是提交的集合中的键。

Additinonally, client entity has a list of orders (added by SQL Metal) as well as a Version field for object tracking. 此外,客户实体具有订单列表(由SQL Metal添加)以及用于对象跟踪的“版本”字段。

When the UpdateModel line gets hit, it doesn't error, but the values in object c don't get updated. 当UpdateModel行被命中时,它不会出错,但是对象c中的值不会被更新。 The problem isn't in service.Update(c) , but in UpdateModel(c, "Client") . 问题不在service.Update(c) ,而在UpdateModel(c, "Client")

What am I doing wrong? 我究竟做错了什么?

Thank you 谢谢

Edit: Client is mapped by SQL metal. 编辑:客户端由SQL金属映射。

Its attributes are as follows: 其属性如下:

  1. int Id 整数ID
  2. String Username; 字符串用户名;
  3. String Firstname; 字符串名字;
  4. String Lastname; 字符串姓氏;
  5. Timestamp Version 时间戳版本
  6. IQuerable Orders; 相当多的订单;

Error (Inner exception is null) 错误(内部异常为null)

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:

The most likely problem is none of the properties start with "Client" . 最可能的问题是所有属性都不以"Client"开头。

Without knowing the detail of your model, it is difficult to say but remove the "Client" and I believe that should fix the problem. 在不知道模型细节的情况下,很难说但是删除"Client" ,我认为这应该可以解决问题。


UPDATE UPDATE

You are likely to have some validation rules. 您可能会有一些验证规则。 Try using TryUpdateModel() which does not do validation on the model. 尝试使用TryUpdateModel()模型进行验证的TryUpdateModel()

I would agree with Sergey that you need to call Save changes in order for this to be persisted. 我会同意谢尔盖(Sergey)的观点,您需要致电“保存更改”,才能将其保留下来。 From your post action I do not see anywhere where this is being persisted to the DB. 从您的发布操作中,我看不到任何持久化到数据库的地方。 You're just calling UpdateModel but there is no SaveChanges that I can see. 您只是在调用UpdateModel,但没有看到SaveChanges。

Hope this helps 希望这可以帮助

您需要在ClientService上提交更改

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM