Link to home
Start Free TrialLog in
Avatar of jeffiepoo
jeffiepooFlag for United States of America

asked on

ASP.NET MVC 3 pass data through controller

Hey Experts, 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}))

Open in new window


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

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

Open in new window


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>

Open in new window


BUT: I'm getting a null exception for the Model.

Please help!

-Jeff
Avatar of kaufmed
kaufmed
Flag of United States of America image

I'll call @Html.Action like this:
Where are you placing this code? Also, what are you trying to accomplish with the partial view? Why are you using it?

You are mixing types as well. In your partial view you declared your model to be a List<int>, but you are passing an int array in your method call.
Avatar of jeffiepoo

ASKER

I am not passing an int[] array, I am supplying an int[] array as a parameter to initialize a List<int> that is used in the method call.

The Partial View is called "SidebarMain", so it is the main sidebar. I am placing the code that generates it (I.E. - Html.Action) in a page that needs a sidebar, but I don't see how this is relevant. I still need to pass data that I manually provide to the dynamically generated partial view.

-Jeff
Sorry if I sounded annoyed. I just re-read my response. Here is the webpage: http://www.mamalennyandtheremedy.com/  SidebarMain is the right hand sidebar. I want to pass in an array of integers in a certain order to the partial view so I can use them to determine which items to display on the right hand side of the page and in what order depending on how the Partial View is called.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have tried this. The reason that I use @Html.Action is because when I use @Html.RenderPartial I always get the error "cannot implicitly convert type 'void' to 'object'"

I've never figured this out. In fact, I posted here on the problem and someone said to use @Html.Action. So that is the reason.

-Jeff
Nevermind :)  

I just saw your use of @{} instead of @. See here:

http://stackoverflow.com/questions/1988358/how-to-transfer-paramenters-to-partial-view-in-asp-net-mvc

I don't know why that is necessary? Not very intuitive.

Thanks! perfect.

-Jeff
It's necessary because you can either execute some logic, or you can have something output in the rendered HTML. If you don't use @{}, then it is assumed that you want something output to the HTML:

<div>
@string.Replace("Hello John World", "John ", "")
</div>

Open in new window


When you are running a method or some logic that shouldn't return anything (i.e. a void method), then you use the braces.
I need some more time.
Here was the solution:

Layout View:
@{Html.RenderPartial("SidebarMain", new int[] {1,3,4,2});}

Open in new window


Partial View:
@model int[]
....
<div id="rightMenuBar">
@foreach( int item in Model){
    switch (item)
    {
        case 1:
         .....

Open in new window


Thanks