0

I'm Completely new in ASP.net MVC

I have two model first is category

public class Category
{
    public virtual int Id { get; set; }
    [Required]
    public virtual string CategoryName { get; set; }
    public virtual List<SubCategory> SubCategories { get; set; }

}

and next subcategory model:

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<Question> Questions { get; set; }

}

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

public class CategorySubCategoryViewMoel
{
    public ICollection<Category> Category { get; set; }
    public SubCategory SubCategory { get; set; }


}

and here is my view

@model FinalSearch5.Models.CategorySubCategoryViewMoel
 <ul>
   @foreach (var category in Model.Category)
   {
     <li>@category.CategoryName</li>
   }
 </ul>

here is my controller

    public ActionResult Index()
    {
        var cats = db.Categories.ToList();
        return View(cats);
    }

It's not implementing completely yet but until here there is an Error

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'.`

Appreciate if help me what is the problem thank you.

8
  • Possible duplicate of Two models in one view in ASP MVC 3 Commented Jul 24, 2016 at 8:56
  • 2
    The error message is self explanatory (your view expects CategorySubCategoryViewMoel but your passing it List<Category> But Category contains a collection of SubCategory so why do you need a view model with 2 separate collections - surely you would want to display each category, and for each category, its collection of SubCategory (i.e. nested foreach loops) Commented Jul 24, 2016 at 9:05
  • 1
    You don't necessarily need a view model if the view is for display only (although its still good practice to use one). The view can be @model List<Category) and then @foreach(var category in Model) { @foreach(var subCategory in category) { ... Commented Jul 24, 2016 at 9:14
  • 1
    By display only, I meant that you are not editing properties of Category or SubCategory in the view (in which case a foreach loop would never have worked anyway). Commented Jul 24, 2016 at 9:46
  • 1
    And that should have read @foreach(var subCategory in category.SubCategories) { Commented Jul 24, 2016 at 9:47

1 Answer 1

2

You view expects a model which is typeof CategorySubCategoryViewMoel but in the Index() method, you return a model which is List<Category>. In order for it to work, you could change the method to

public ActionResult Index()
{
    var model = new CategorySubCategoryViewMoel
    {
        Category = db.Categories.ToList()
    }
    return View(model );
}

Note that the property is a collection, so its name should be pluralized, and need only be IEnumerable i.e. public IEnumerable<Category> Categories { get; set; }

However its unclear what the purpose of your view model is. Its only other property is public SubCategory SubCategory { get; set; } and you never assign a value to it, or use it in the view. You code could use your current Index() method and then the view would be

@model List<Category>
<ul>
    @foreach(var category in Model)
    {
        <li>
            @category.CategoryName
            <ul>
                @foreach(var subCategory in category.SubCategories)
                {
                    <li>@subCategory.SubCategoryName</li>
                }
            </ul>
        <li>
    }
<ul>
Sign up to request clarification or add additional context in comments.

Comments

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.