Update entity with ViewModel and Entity Framework 6?

北城以北 提交于 2020-01-15 05:40:09

问题


I've been looking around and can't quite find the answer. I'm using a ViewModel in my Edit View so that I can have values for some dropdownlist. Now when I go to update my DB I'm not understanding how I can update my database record. I'm guessing I could create a new entity object, do a Find, and then update each property based on the ViewModel passed in from the Form but that sure seems like a lot of manual work.

Here I'm using the VeiwModel in the Edit View.

@model CPPCustomerCall.ViewModels.CustomerCallVM

Here is my controller's ActionResult. I changed the object type of the ActionResult to take in CustomerCallVM instead of the CustomerCall which was auto-generated. I assume since the Edit View's model is the ViewModel that's the type of object the ActionResult will receive. However, my ViewModel has more properties that aren't needed for the Entity Model to update the record. How do I go about updating my DB record in this ActionResult?

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit([Bind(Include = "Id,CustomerName,Subject,PhoneNumber,CallMessage,CallDate,Status,CallNotes")] CustomerCallVM customerCall)
{
    if (ModelState.IsValid)
    {
        db.Entry(customerCall).State = EntityState.Modified;
        await db.SaveChangesAsync();
        return RedirectToAction("Index");
    }
    return View(customerCall);
}

回答1:


First, Bind and view models are mutually exclusive. If you don't want something to be eligible to be modified, then it shouldn't be on your view model, in the first place. Barring that, view models diverge from entities in the sense that they can't be saved directly. As a result, there's always some intervention present on your part to map the posted values back onto the entity, which means you can then selectively not map over certain properties that shouldn't be, regardless of whether they were posted or not. Long and short, get rid of the Bind stuff. It's just something else to maintain and a huge source of potential bugs.

That said, the code you have is workable; you're just missing the crucial part where you map the data from your view model back onto your entity. First, you need to fetch the entity from the database so you have a base to work from:

var customerCall = db.CustomerCalls.Find(id);
if (customerCall == null)
{
    return new HttpNotFoundResult();
}

FWIW, your edit route should include the id in the route, according to REST conventions. Following REST isn't strictly required, but it's certainly recommended. While a web application adhering to REST doesn't mean it's a good application, not adhering to rest is generally a sure sign of a badly designed and coded application.

Then, you map over your properties. You can either do this manually:

customerCall.CustomerName = model.CustomerName;
// etc.

Or you can use a library like AutoMapper.

mapper.Map(model, customerCall);

AutoMapper requires a bit of initial setup to make this magic work, of course, so review the docs, if you're going that route. Manual mapping is easier, but far more tedious and repetitive.




回答2:


[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit([Bind(Include = "Id,CustomerName,Subject,PhoneNumber,CallMessage,CallDate,Status,CallNotes")] CustomerCallVM customerCall)
{
    if (ModelState.IsValid)
    {
       // Find The record you need
       var dbObj = CustomerCalls.FirstOrDefault(x=> x.id = customerCall.id);
       //Check if null
       if(dbObj == null) dbObj = new CustomerCall();

       /// Map your properties

       // Add object to the stack
       if(dbObj.id == 0){
         CustomerCalls.Add(dbObj);
       }else{
         CustomerCalls.Update(dbObj);
       }

    await db.SaveChangesAsync();
    return RedirectToAction("Index");
    }
    return View(customerCall);
}


来源:https://stackoverflow.com/questions/37278517/update-entity-with-viewmodel-and-entity-framework-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!