Note that there are some explanatory texts on larger screens.

plurals
  1. POScaffolding ModelView creates underlying database tables
    primarykey
    data
    text
    <p>I'm trying to use ViewModels for the first time using AutoMapper. I have two models:</p> <pre><code>public class Item { public int ItemId { get; set; } public bool Active { get; set; } public string ItemCode { get; set; } public string Name { get; set; } public List&lt;ItemOption&gt; ItemOptions { get; set; } //... } public class ItemOption { public int ItemOptionId { get; set; } public string Name { get; set; } public string Barcode { get; set; } //... } </code></pre> <p>Which I have turned into two ViewModels:</p> <pre><code>public class ItemDetailViewModel { public int ItemDetailViewModelId { get; set; } public int ItemId { get; set; } public bool Active { get; set; } public string ItemCode { get; set; } public string Name { get; set; } public List&lt;ItemDetailItemOptionViewModel&gt; ItemOptions { get; set; } } public class ItemDetailItemOptionViewModel { public int ItemDetailItemOptionViewModelId { get; set; } public int ItemOptionId { get; set; } public string Name { get; set; } public string Barcode { get; set; } } </code></pre> <p>I then set the following in my application start-up:</p> <pre><code>Mapper.CreateMap&lt;Item, ItemDetailViewModel&gt;(); Mapper.CreateMap&lt;ItemOption, ItemDetailItemOptionViewModel&gt;(); </code></pre> <p>Finally I scaffolded my ItemDetailViewModel:</p> <p><img src="https://i.stack.imgur.com/IdASh.png" alt="enter image description here"></p> <p>I then built my project and added a new Item through /Item/Create</p> <p>I had a look in the database expecting to see that I would have an entry in the Item table, but instead I have ItemDetailViewModel and ItemDetailItemOptionViewModel tables, which I wasn't expecting and the data is is ItemDetailViewModel.</p> <p>I assume I have done something wrong with my scaffolding? How do I scaffold off the ViewModel without making it part of the main business models?</p> <p><strong>Further Details</strong></p> <p>If it isn't possible to scaffold the controller with a ViewModel, then how do I reference the ViewModel in the controller and save changes back to the database?</p> <p>For example what would the following change to once I remove ItemDetailViewModel from the db context?</p> <pre><code> // // POST: /Item/Create [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(ItemDetailViewModel itemdetailviewmodel) { if (ModelState.IsValid) { db.ItemDetailViewModels.Add(itemdetailviewmodel); db.SaveChanges(); return RedirectToAction("Index"); } return View(itemdetailviewmodel); } </code></pre> <p><strong>Further Details [2]</strong></p> <p>So am I correct that my Index/Details should work as so or is there a better way of doing it?</p> <pre><code> // // GET: /Item/ public ActionResult Index() { var items = db.Items.ToList(); var itemdetailviewmodel = AutoMapper.Mapper.Map&lt;ItemDetailViewModel&gt;(items); return View(itemdetailviewmodel); } // // GET: /Item/Details/5 public ActionResult Details(int id = 0) { ItemDetailViewModel itemdetailviewmodel = AutoMapper.Mapper.Map&lt;ItemDetailViewModel&gt;(db.Items.Find(id)); if (itemdetailviewmodel == null) { return HttpNotFound(); } return View(itemdetailviewmodel); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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