Persistir el objeto Binded Model utilizando el marco de la entidad
-
20-09-2019 - |
Pregunta
Intentaré mantener esto corto y conciso.
Tengo mi controlador aquí ...
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(CustomObject myCustomObject)
{
...
}
Donde mycustomobject se ve genial. Pero, si quiero guardar esto usando el marco de la entidad, necesito hacer algo como esto ...
[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();
}
¿Hay alguna forma de evitar hacer este ejercicio de mapeo?
Solución
[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();
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow