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 に答える