엔티티 프레임 워크를 사용하여 모델 바인드 객체를 지속합니다
-
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