Persistant l'objet modèle bindi
-
20-09-2019 - |
Question
J'essaierai de garder cela court et concis.
J'ai mon contrôleur ici ...
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(CustomObject myCustomObject)
{
...
}
Où MyCustomObject a fière allure. Mais, si je veux enregistrer ceci en utilisant le cadre d'entité, j'ai besoin de faire quelque chose comme ça ...
[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();
}
Existe-t-il un moyen de ne pas faire de cette mappage d'exercice?
La solution
[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();
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow