الاستمرار في كائن نموذج محدد باستخدام إطار الكيان
-
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();
}
هل هناك طريقة يمكنني الاحتفاظ بها من القيام بهذا رسم الخرائط؟
المحلول
[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