Solved

ASP.NET MVC 3 pass data through controller

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

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
 
LVL 74

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 74

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now