ASP.NET MVC - MasterPageView and RenderPartials - Confusion

I got a little confused trying to make a list of categories in the navigation bar on MasterPageView in the latest version of ASP.NET MVC. I have 0 experiences with Partials so far (this adds to the confusion).

Should I use this RenderPartial option?

HtmlHelper.RenderPartial(string partialViewName, object model)

I could not find good examples of this method. By agreement, there is no model associated with the MasterPageView right? So, what is the right way to pop or pull data from the "partial" from MasterPageView?

Assuming this method is absolutely going the wrong way:

    <div id="navigation">
        <% 
            CategoryRepository cr = new CategoryRepository();
            IList<Category> lst = cr.GetCategories();
            Html.RenderPartial("NavBar", lst);
        %>
    </div>
+5
source share
4 answers

, viewdata? , , , viewdata ...

BaseViewData.cs - viewdata,

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

Site.Master

<%@ 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) %>

, .

HTHS,

+7
public ActionResult NavBar()
{

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


            return View(lst);
}

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

ui

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

ActionResult

+1

, , , , , , ViewData . , , . , , - :

ViewData["MasterPageData"] = FunctionToGetData();

- :

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

Then, in the control, as well as on the normal viewing page, do the following:

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

process as normal...

I have not yet had to transfer data to the main page, but I would have thought that you would. More details here.

EDIT: Modified it a bit to reflect what I'm doing in my current project.

+1
source

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

0
source

All Articles