Kevin himch
Kevin himch

Reputation: 297

Pass list of categories to partial view

I have a partial view which will display list of Main Categories and under each Main Category all of its subcategories. But the problem is I don't know how can I pass this Category List to my partial view. Please check the code bellow. I've also attached my .edmx table map picture to give you better idea. Once I pass it to partial view I want to loop though all categories and sub categories to display them

pic

[ChildActionOnly]
public PartialViewResult _GuestNav()
{
    using (var db = new TestEntities())
    {
        db.Categories.ToList(); // get list from here

        return PartialView("_GuestNav"); // then pass that list to partial view
    }
}

Upvotes: 0

Views: 6955

Answers (3)

Rey
Rey

Reputation: 4002

Here is the main action code:

public ActionResult Categories()
{
    using (var dbCtx = new DbContext())
    {
        var categories = dbCtx.Categories.Include(x => x.SubCategories).ToList()
        return View(categories); 
    }
}

Then in your Categories.cshtml you will have the code as below:

@model IEnumerable<Categories>

<ul>
    @foreach(var category in Model)
    {
        <li>@category.CategoryName
         @if(category.SubCategories.Any())
         {
            Html.RenderPartial("~/Partial/_SubCategory.cshtml", category.SubCategories); 
         }
         </li>
    }
</ul>

At last you supply a partial view called _SubCategory.cshtml in the Partial folder of Category folder as below:

 @model IEnumerable<SubCategory>
 <ul>
    @foreach(var subCategory in Model)
    {
        <li>@subCategory.SubCategoryName</li>
    }
</ul>

In your case if you want to pass this list to the partial view you specified you can do it as below:

[ChildActionOnly]
public PartialViewResult _GuestNav()
{
    using (var db = new TestEntities())
    {
       var categories = db.Categories.Include(x => x.SubCategories).ToList(); // Added the include if you want to add subcategories as well
        return PartialView("_GuestNav", categories); // then pass that list to partial view
    }
}

Upvotes: 1

Matt M
Matt M

Reputation: 3779

The PartialView method has an override that accepts an object. You need to store the results of the db.Categories.ToList() call in a variable and pass that to the method like this:

 using (var db = new TestEntities())
{
    var cats = db.Categories.Include("SubCategories").ToList(); // get list from here

    return PartialView("_GuestNav", cats); // then pass that list to partial view
}

Just make sure your partial view expects a list of categories as its model. Then, inside your view you can iterate over the model and display the subcategories.

You should also look into how to use a viewmodel for your views.

EDIT

You may need to use an include statement since navigation properties are generally lazy loaded. Updated my answer.

Upvotes: 0

Emilio Lucas Ceroleni
Emilio Lucas Ceroleni

Reputation: 1598

Yo can use model binding, pass a Model or ViewModel as a parameter and access it from the partial view. For example, in your _GuestNav action:

...
return PartialView("_GuestNav",db.Categories.ToList());

Here's a link on how to accomplish that.

Then you can bind the model in your view. For example:

...
@model IEnumerable<Categories>;

For more detail, check out the examples from the link.

Upvotes: 0

Related Questions