Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Scaffolding is not that intelligent. The standard controller scaffolding template is creating a DbContext with the controller model and presumes you are working with the DB models, not view models and it does not use Automapper. So you'll need to either not use scaffolding, or check what it has done before using it.</p> <p>And nothing is wrong with the way you use scaffolding, it is just not supposed to do what you expect.</p> <p><strong>Update</strong> this is how you do this without scaffolding</p> <pre><code>// Without Automapper [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(ItemDetailViewModel model) { if (ModelState.IsValid) { var item = new Item() { Active = model.Active, ItemCode = model.ItemCode, Name = model.Name, ItemOptions = // code to convert from List&lt;ItemDetailItemOptionViewModel&gt; to List&lt;ItemOption&gt; } db.Items.Add(item); db.SaveChanges(); return RedirectToAction("Index"); } return View(model); } // with Automapper - not recommended by author of Automapper [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(ItemDetailViewModel model) { if (ModelState.IsValid) { var item = Automapper.Mapper.Map&lt;Item&gt;(model); db.Items.Add(item); db.SaveChanges(); return RedirectToAction("Index"); } return View(model); } </code></pre> <p>You'll need to modify your DbContext to have <code>IDbSet&lt;Item&gt; Items</code> instead of <code>IDbSet&lt;ItemDetailViewModels&gt; ItemDetailViewModels</code>. </p> <p>Automapper was created to map from Domain Models to View Models and not the other way. I have done that for a while, but this is troublesome and causes subtle bugs and other maintenance problems. Even Jimmy Bogard himself <a href="http://lostechies.com/jimmybogard/2009/09/18/the-case-for-two-way-mapping-in-automapper/" rel="nofollow">says you should not map from view models to domain models</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload