views:

49

answers:

3

I'm still sadly a bit n00bish when it comes to .NET MVC. I have a navigation partial view being called from a MasterPage where I would like to get a list of data. This partial view is in the "Shared" folder, and thus does not have a controller. How do I get data to it so that it may render a list?

Thanks

More specifically, I want to do something like this (pseudocode):

<ul>
<% foreach (item in ListOfItems) {
    Response.Write(formattedListItem);
} %>
</ul>
+1  A: 

[Edit] New helpful link now that I know the full story

http://www.superexpert.com/blog/archive/2008/08/12/asp-net-mvc-tip-31-passing-data-to-master-pages-and-user-controls.aspx

pdr
this is all well and good, but i need it for a SHARED partial view. i can get data for one that isn't shared.... :/
Jason
Why would it be different?
pdr
i'm calling the shared view from my main master page which has no controller nor data to pass to it. i need to get it data... sometimes mvc can be so frustrating
Jason
Right, so what you really want to know is how to pass data to the Master Page. There are a mix of ways to do that, none of them nice. I tend to have an interface on any viewdata class that goes to a view using the master, then use Inherits="ViewMasterPage<IMasterDataContainer>" on the master page.
pdr
A: 

You pass in a view model just like you do for a regular page. Only, instead of passing it in when calling View(...) in the controller, you use a helper method. Something like:

<% Html.RenderPartial("~/Views/Shared/Navigation.ascx", Model.MenuItems); %>

The data you are passing in (Model.MenuItems in this case) will probably come from the Model of the View in which you are using the partial view (as is the case in the example above).

Rune
Well, this is being called from a masterpage, which doesn't have access to any data...
Jason
A: 

Just use RenderAction. RenderAction will call the method on a controller you choose and get the results you wish to display. You can even return a PartialView from this method.

<% Html.RenderAction("actionName", "controllerName"); %>

Hope that helps.

Cosmo
i don't get a `RenderAction` option in intellisense from `Html.`
Jason