10

Here is my controller code, which works 100% as I need it to. However the POST method isn't using the AutoMapper and that is not OK. How can I use AutoMapper in this action method?

I'm using Entity Framework 4 with the Repository Pattern to access data.

public ActionResult Edit(int id)
{
    Product product = _productRepository.FindProduct(id);
    var model = Mapper.Map<Product, ProductModel>(product);
    return View(model);
}

[HttpPost]
public ActionResult Edit(ProductModel model)
{
    if (ModelState.IsValid)
    {
        Product product = _productRepository.FindProduct(model.ProductId);

        product.Name = model.Name;
        product.Description = model.Description;
        product.UnitPrice = model.UnitPrice;

        _productRepository.SaveChanges();

        return RedirectToAction("Index");
    }

    return View(model);
}

If I use AutoMapper, the entity framework reference is lost and the data doesn't persist to the database.

[HttpPost]
public ActionResult Edit(ProductModel model)
{
    if (ModelState.IsValid)
    {
        Product product = _productRepository.FindProduct(model.ProductId);
        product = Mapper.Map<ProductModel, Product>(model);

        _productRepository.SaveChanges();

        return RedirectToAction("Index");
    }

    return View(model);
}

I'm guessing this is caused the Mapper.Map function returning a brand new Product object and because of that, no references to the entity framework graph is being kept. What alternatives do you suggest?

6
  • It's not clear what the problem you are experiencing is. You say that your POST method is not using Automapper, but I don't see any Automapper code in your [HttpPost] method. Commented Jan 13, 2012 at 0:40
  • you're probably not posting back the right thing? Commented Jan 13, 2012 at 0:42
  • Not sure he means automapper Robert, think he means "modelbinder" but I'm not 100% sure Commented Jan 13, 2012 at 0:44
  • I edited in more details that should make the problem clearer. I don't mean ModelBinder, I mean the actual lib called AutoMapper. Commented Jan 13, 2012 at 0:44
  • oh, you want the map to map to an EXISTING object... what you are doing is turning a ProductModel into a new Product object. Commented Jan 13, 2012 at 0:47

1 Answer 1

15

I think you just do

 Product product = _productRepository.FindProduct(model.ProductId);
 Mapper.Map(model, product);
 _productRepository.SaveChanges();

you may also want to check that you have a non null product first, and also that user is allowed to change that product....

Sign up to request clarification or add additional context in comments.

1 Comment

Its correct , Actually we have to create Map for Get and Post of Edit Method , for Get Its: Domain Model To ViewModel Mappings and for Post Its: ViewModel To Domain Model Mappings , check this, hope helps someone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.