0

我会尽量保持简短和简洁。

我的控制器在这里...

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

有没有办法可以避免做这个映射练习?

4

1 回答 1

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();
}
于 2009-11-16T00:22:08.527 回答