2009-08-26 69 views
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(); 
} 

有沒有一種方法,我可以繼續從做這個映射excersise?

回答

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