title: asp mvc list product details in view using View Model
body: <p>I am trying to list single product details in a view. The product specification changes dynamically because specifications are added row-wise in table, which means we can add huge number of specifications for each product (as done in ecommerce sites).  Right now I am able to meet the requirement using <code>ViewBag</code>, but I am deciding to use <code>ViewModel</code> as a better practice.</p>
  
  <p><strong>Model class:</strong></p>
  
  <pre><code>// Product:
  public partial class ProductTable
  {
      public ProductTable()
      {
          this.SpecificationsTable = new HashSet&lt;SpecificationsTable&gt;();
      }
  
      public int ProductID { get; set; }
      public string Title { get; set; }
      public string SmallDescription { get; set; }
      public string FullDescription { get; set; }
  
      public virtual ICollection&lt;SpecificationsTable&gt; SpecificationsTable { get; set; }
      }
  
  //Specifications:
  public partial class SpecificationsTable
  {
      public int SpecificationsID { get; set; }
      public string SpecificationName { get; set; }
      public string SpecificationValue { get; set; }
      public Nullable&lt;int&gt; ProductID { get; set; }
      public virtual ProductTable ProductTable { get; set; }
  }
  </code></pre>
  
  <p><strong>ViewModel:</strong></p>
  
  <pre><code>public class DetailsViewModel
  {
      public int ProductID { get; set; }
      public string Title { get; set; }
      public string SmallDescription { get; set; }
      public string FullDescription { get; set; }
      public string SpecificationName { get; set; }
      public string SpecificationValue { get; set; }
  }
  </code></pre>
  
  <p><strong>ActionMethod</strong></p>
  
  <pre><code>public ActionResult ProductDetails(int id)
  {
      var details = (from c in dbo.ProductTable
                     join s in dbo.SpecificationsTable 
                     on c.ProductID equals s.ProductID
                     where c.ProductID == id
                     select new DetailViewModel
                     {
                         Title = c.Title,
                         SmallDescription = c.SmallDescription,
                         FullDescription = c.FullDescription
                     }).ToList();
  
       // To remove repeated product title , small and full description
       var distinctItems = details.GroupBy(x =&gt; x.ProductID).Select(y =&gt; y.First());
  
       // To show product title, small and full description for this product
  
       ViewBag.ProductDetails = distinctItems;
  
       var specifications = (from c in dbo.ProductTable
                               join s in dbo.SpecificationsTable 
                               on c.ProductID equals s.ProductID
                               where c.ProductID == id
                               select new DetailViewModel
                               {
                                   SpecificationName = s.SpecificationName,
                                   SpecificationValue = s.SpecificationValue
                               }).ToList();
  
      // To show list of specifications for this product
      ViewBag.Specifcations = specifications;
      return View();
  }
  </code></pre>
  
  <p><strong>expected output in view:</strong></p>
  
  <p><strong>Details:</strong>
  </p>
  
  <pre><code>Title: New Samsung offer
  
  SmallDescription : Something small 
  
  FullDescription : Something full
  </code></pre>
  
  <p><strong>Specifcations:</strong>
  </p>
  
  <pre><code>Mobile Name :Samsung
  
  Model : 2015
  
  Price : 70 $
  
  Color:  White
  </code></pre>
  
  <p>I am using database first method and I am trying to learn how we can use view model here.</p>