使用Entity Framework保留模型绑定对象

时间:2009-08-26 19:09:47

标签: entity-framework model-binding

我会尽量保持简洁明了。

我的控制器在这里......

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(CustomObject myCustomObject)
{
     ...
}

myCustomObject看起来很棒。但是,如果我想使用实体框架保存它,我需要做这样的事情......

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(CustomObject myCustomObject)
{
     CustomObject existingObject = repository.GetCustomObject(myCustomObject.ID);

     // Set all the attributes of myCustomObject to existingObject
     existingObject.SomeMapperFunction(myCustomObject)

     repository.Save();
}

有没有办法可以阻止这种映射工作?

1 个答案:

答案 0 :(得分:0)

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id)
{
     CustomObject existingObject = repository.GetCustomObject(id);

     TryUpdateModel(existingObject);
     // You additionaly can check the ModelState.IsValid here

     repository.Save();
}