2

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?

Sand
  • 51
  • 2
  • 8

1 Answers1

4

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 :)

Yngve B-Nilsen
  • 9,606
  • 2
  • 36
  • 50
  • PartialViewResult has no Parameter and RenderAction send me an Compilation Error :-) – Sand Apr 15 '11 at 12:03
  • With following Solution i had success: http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx - Thanks Nilsen – Sand Apr 15 '11 at 12:10