エンティティフレームワークを使用してモデルバインドオブジェクトを永続化します
-
20-09-2019 - |
質問
これを短く簡潔にしようとします。
ここにコントローラーを手に入れました...
[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();
}
このマッピングExcersiseを実行しないようにする方法はありますか?
解決
[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();
}
所属していません StackOverflow