Persistendo l'oggetto binded modello utilizzando Entity Framework
-
20-09-2019 - |
Domanda
Cercherò di mantenere questo breve e concisa.
ho ottenuto il mio regolatore di qui ...
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(CustomObject myCustomObject)
{
...
}
Dove myCustomObject sembra grande. Ma, se voglio salvare questo utilizzando il framework entità, ho bisogno di fare qualcosa di simile ...
[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();
}
C'è un modo per evitare di fare questa mappatura excersise?
Soluzione
[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();
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow