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.
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
In short: your code is missing the items parameter name in the Html.Action().
Other than that the code should be functional.
Html.Action("SidebarMain", "Home", new {items = new List<int>(new int[] {1, 2, 3}) })
As a suggested practice, i would use a dedicated ViewModel in my view rather than just sending the array of integers. Because, in this way of a clean ViewModel - a container of your properties that you display in the view, your code may add other properties later on, as our code always evolves.
Reference to the usage of a ViewModel concept: Exercise 5: Creating a View Model
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With