title: Implement a collection of an object in my view model - ASP.net MVC
body: <p>I'm Completely new in ASP.net MVC</p>
  
  <p>I have two model first is category </p>
  
  <pre><code>public class Category
  {
      public virtual int Id { get; set; }
      [Required]
      public virtual string CategoryName { get; set; }
      public virtual List&lt;SubCategory&gt; SubCategories { get; set; }
  
  }
  </code></pre>
  
  <p>and next subcategory model:</p>
  
  <pre><code>public class SubCategory
  {
      public virtual int Id { get; set; }
  
      public virtual int CategoryId { get; set; }
      [Required]
      public virtual string SubCategoryName { get; set; }
      public virtual Category Category { get; set; }
      public virtual List&lt;Question&gt; Questions { get; set; }
  
  }
  </code></pre>
  
  <p>I need to have a list of my categories and also a list of subcategories both in my view so this is my view model</p>
  
  <pre><code>public class CategorySubCategoryViewMoel
  {
      public ICollection&lt;Category&gt; Category { get; set; }
      public SubCategory SubCategory { get; set; }
  
  
  }
  </code></pre>
  
  <p>and here is my view </p>
  
  <pre><code>@model FinalSearch5.Models.CategorySubCategoryViewMoel
   &lt;ul&gt;
     @foreach (var category in Model.Category)
     {
       &lt;li&gt;@category.CategoryName&lt;/li&gt;
     }
   &lt;/ul&gt;
  </code></pre>
  
  <p>here is my controller </p>
  
  <pre><code>    public ActionResult Index()
      {
          var cats = db.Categories.ToList();
          return View(cats);
      }
  </code></pre>
  
  <p>It's not implementing completely yet but until here there is an Error </p>
  
  <blockquote>
    <p>The model item passed into the dictionary is of type 'System.Collections.Generic.List1[FinalSearch5.Models.Category], but this dictionary requires a model item of type 'FinalSearch5.Models.CategorySubCategoryViewMoel'.`</p>
  </blockquote>
  
  <p>Appreciate if help me what is the problem thank you.</p>