Search code examples
c#asp.net.netasp.net-mvc-3asp.net-mvc-3-areas

Passing data from Html.Action to the partial view


I'm still relatively new to MVC 3. I need to pass data from my @Html.Action methods through the controller to a partial view.

So here is my flow.

I'll call @Html.Action like this:

@Html.Action("SidebarMain", "Home", new List<int>(new int[] {1, 2, 3}))

Then it will hit my Controller. Here is my method in my Home Controller:

public ActionResult SidebarMain(List<int> items)
{
    return View(items);
}

Then my Partial View should be able to access the data like so:

@model List<int>

@{
    ViewBag.Title = "SidebarMain";
    Layout = null;
}

<div>
@foreach (int item in Model)
{
    <div>@item</div>
}
</div>

BUT: I'm getting a null exception for the Model, meaning It's not passing through.


Solution

  • Try this:

    Html.Action("SidebarMain", "Home", new { items = new List<int>(new int[] {1, 2, 3}) })
    

    And put a breakpoint in your SidebarMain Action to see, if you are getting items