tags:

views:

283

answers:

4

I'm a little confused with trying to do bring a list of Categories into a navigation bar on a MasterPageView in the latest release of the ASP.NET MVC framework. I have 0 experience with Partials so far (this adds to the confusion).

Should I use this variant of the RenderPartial?

HtmlHelper.RenderPartial(string partialViewName, object model)

I wasn't able to find any good examples of this method. By convention there is no model associated with the MasterPageView right? So what is the proper way to push or pull data into a "partial" from the MasterPageView?

Assuming that this method is absolutely going down the wrong path:

    <div id="navigation">
        <% 
            CategoryRepository cr = new CategoryRepository();
            IList<Category> lst = cr.GetCategories();
            Html.RenderPartial("NavBar", lst);
        %>
    </div>
A: 

I would use Html.RenderAction() instead and return a partial view from it.

John Sheehan
Where do you get the RenderAction() method from? Is this a Contrib thing?
tyndall
It might be in MVC Futures on CodePlex. I don't recall off hand (not close to where I can check right now)
John Sheehan
+1  A: 
public ActionResult NavBar()
{

            CategoryRepository cr = new CategoryRepository();
            IList<Category> lst = cr.GetCategories();


            return View(lst);
}

on your partial call

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%@ Import Namespace="app.Models" %>

and do all your rendering ui here

<div id="navigation">
        <% 
           Html.RenderPartial("NavBar");
        %>
    </div>

you can do ActionResult calls in your controllers

Ayo
On every single one of my controllers I have to do this NavBar() call? or inherit it in... that doesn't seem right.
tyndall
what i do is create BaseController that inherits Controller and put all my reuseable partial code calls in the base controller and have all my controllers inherit the BaseController. That way all controllers have access to that partial.
Ayo
No, you dont have to. just put your navigation code @ masterpage file
Funky81
+1  A: 

I would say that since it's the Master Page you would probably have to store your data that you are passing in ViewData with a string key. If it was a regular view page it would be better to have a strongly typed page, but this is a different case. So you would probably do something this in your controller:

ViewData["MasterPageData"] = FunctionToGetData();

And then on the Master Page something like this:

<% 
   if (ViewData["MasterPageData"] != null) 
   {
      Html.RenderPartial("ControlName.ascx", ViewData);
   } 
%>

Then in the control, process like you would on a normal view page:

<% var categories = (CastIfNeeded)ViewData["MasterPageData"]; %>

process as normal...

I haven't had to pass data to a master page yet, but that's how I would think you'd do it. More info here.

EDIT: Changed it around a little to reflect what I'm doing in my current project.

dhulk
+1 good info. Let me ask you this in your example... your passing data in as the [object] model. How do you reference it from the Partial-side? The tutorial shows the ViewData method.
tyndall
Edited to (hopefully) answer your question.
dhulk
+7  A: 

Do you not want your masterpage to have viewdata? You could solve it by having a base view data class that ALL your other viewdata classes inherit from...

BaseViewData.cs - this is a viewdata class that all other viewdata classes will inherit from

public class BaseViewData
{
    public string Title { get; set; }
    public string MetaKeywords { get; set; }
    public string MetaDescription { get; set; }
    IList<Category> NavCategoryList { get; set; }
}

Now in your Site.Master page simply have

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<BaseViewData>" %>

<title><%=ViewData.Model.Title %></title>
<meta name="keywords" content="<%=ViewData.Model.MetaKeywords %>" />
<meta name="description" content="<%=ViewData.Model.MetaDescription %>" />

<%= Html.RenderPartial("NavBar", ViewData.Model.NavCategoryList) %>

This could significantly impact your application architecture, but its not necessarily a bad thing.

HTHs, Charles

Charlino
Yeah I follow this method.
Graphain
+1 Hmmm. This might work... and at least now I know the "ViewData.Model." syntax. Not sure why I hadn't seen that yet.
tyndall
The Kigg project does it this way, I am about to implement it. Where and how do you load the Model so that the master page can do this: ViewData.Model.Titl
Picflight