Solved

ASP.NET MVC 3 pass data through controller

Posted on 2013-01-19
9
488 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

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Graphics 2 30
Expression Evaluater 3 27
No Data for DropDown List 2 26
c# - Best approach for objects in functions 3 17
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

803 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