Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ASP.NET MVC 3 pass data through controller

Posted on 2013-01-19
9
Medium Priority
?
494 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Suggested Courses

972 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