Search code examples
asp.net-mvc-3modelrenderpartial

ASP.Net MVC3 Menu with different Model as Content (partial view, renderpage?)


i have a page with a fix menu on the left side. This Partial View needs a different Model as the Main Page (Content).

Masterpage/Layout:

<body>
<div id="IndexMenu">
    <div id="IndexMenuInner">@RenderPage("~/Views/Admin/part/_Menu.cshtml", new { LocationAdminModelCollection = new Model; })</div>
</div>
<div id="BodyContent">
    @RenderBody()
</div>

Index/Content Page which where called at start:

@model Survey.WebApplication.Models.ChecklistDetailsModel

@{
    ViewBag.Title = "Survey Administration";
    Layout = "~/Views/Admin/_Layout.cshtml";
}

<link href="@Url.Content("~/Content/Admin/Menu.css")" rel="stylesheet" type="text/css" />

<div id="IndexSubMenu">sub_Menu</div>

<div>
<div id="IndexMenuInner"></div>
</div>

My Menu:

@model Survey.WebApplication.Models.LocationAdminModelCollection

@{
    Layout = null;
}

<div class="menuLocation">

</div>

Ho can i do this?


Solution

  • I would use Html.RenderAction to render an Action on your Controller. In that action, you simply create the Model that your Menu needs, and pass out the Menu.cshtml partial View as a PartialViewResult

    So instead of @RenderPage("~/Views/Admin/part/_Menu.cshtml", new { LocationAdminModelCollection = new Model; })

    you'd do:

    @{ Html.RenderAction("Menu", "Site"); }
    

    Where Site is your SiteController and Menu is something like:

    public ActionResult Menu()
    {
        return PartialView("Menu", new { LocationAdminModelCollection = new Model });
    }
    

    Disclaimer

    Code not tested :)