Solved

ASP.NET MVC 3 pass data through controller

Posted on 2013-01-19
9
489 Views
Last Modified: 2013-05-31
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
0
Comment
Question by:jeffiepoo
  • 6
  • 3
9 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38797805
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.
0
 
LVL 6

Author Comment

by:jeffiepoo
ID: 38797976
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
0
 
LVL 6

Author Comment

by:jeffiepoo
ID: 38797982
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.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 38798108
My experience with partial views has been that you add a call to Html.RenderPartial within a view. That causes the partial view to be rendered as a part of the main view. When you call Html.RenderPartial, you pass in the name of the partial view, and you can also pass in model data for the partial view to use. In order to do this, the types must match. I'm not sure what you are using Html.Action for.

Here's a simplified example:

Index.cshtml
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
@{Html.RenderPartial("SidebarMain", new int[] { 1, 2, 3 });}

Open in new window


SidebarMain.cshtml
@model int[]

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

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

Open in new window


HomeController.cs
using System.Web.Mvc;

namespace _28001643.Controllers
{
    public class HomeController : Controller
    {
        // GET: /Home/
        public ActionResult Index()
        {
            return View();
        }
    }
}

Open in new window


This controller and action causes the Index view to be sent to the client. During the rendering, Index.cshtml will invoke the partial view.
0
 
LVL 6

Author Comment

by:jeffiepoo
ID: 38798814
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
0
 
LVL 6

Author Comment

by:jeffiepoo
ID: 38798822
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
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38799377
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.
0
 
LVL 6

Author Comment

by:jeffiepoo
ID: 39085247
I need some more time.
0
 
LVL 6

Author Comment

by:jeffiepoo
ID: 39212080
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
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question