Das Modell des Modells bestehen, das das Objekt mithilfe des Entity Framework beherrscht

StackOverflow https://stackoverflow.com/questions/1336862

  •  20-09-2019
  •  | 
  •  

Frage

Ich werde versuchen, diese kurze und präzise zu halten.

Ich habe meinen Controller hier ...

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(CustomObject myCustomObject)
{
     ...
}

Wo Mycustomobject toll aussieht. Aber wenn ich dies mit dem Entity -Framework speichern möchte, muss ich so etwas tun ...

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

Gibt es eine Möglichkeit, diese Mapping -Auszüge abzuhalten?

War es hilfreich?

Lösung

[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();
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top