How to update Model when binding to a ViewModel?

前端 未结 3 1536
予麋鹿
予麋鹿 2021-02-01 22:42

I have an [HttpPost] action method signature like this:

[HttpPost]
public ActionResult Edit(ExistingPostViewModel model)
{
   // Save the edited Pos         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-01 23:14

    Not sure if this will help, but it is working for me. I have my underlying domain table as a Visitor Object. My viewmodel contains the Visitor Object plus a couple of IEnumerables for dropdowns.

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id)
    
        {
            Visitor Visitor = new Visitor();
            Visitor = db.Visitors.FirstOrDefault(v => v.VisitorID == id);
    
            UpdateModel(Visitor, "Visitor");
    
            db.SaveChanges();
            return RedirectToAction("Index");
    
        }
    

    The UpdateModel works off my viewmodel because of the "Visitor" string telling it what values to compare.

提交回复
热议问题