asp.net-mvc-3 - ASP.Net MVC3 菜单以不同的模型作为内容(部分 View ,渲染页面?)

标签 asp.net-mvc-3 model renderpartial

我有一个页面,左侧有一个修复菜单。此部分 View 需要不同的模型作为主页(内容)。

母版页/布局:

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

开始时调用的索引/内容页面:

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

我的菜单:

@model Survey.WebApplication.Models.LocationAdminModelCollection

@{
    Layout = null;
}

<div class="menuLocation">

</div>

我可以这样做吗?

最佳答案

我将使用 Html.RenderAction 在 Controller 上呈现操作。在该操作中,您只需创建菜单所需的模型,并将 Menu.cshtml 部分 View 作为 PartialViewResult 传递出去

因此,不要使用 @RenderPage("~/Views/Admin/part/_Menu.cshtml",而是 new { LocationAdminModelCollection = new Model; })

你会这样做:

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

其中 Site 是您的 SiteController,Menu 类似于:

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

免责声明

代码未测试:)

关于asp.net-mvc-3 - ASP.Net MVC3 菜单以不同的模型作为内容(部分 View ,渲染页面?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5676184/

相关文章:

asp.net-mvc - 无法更新标识列 'Unique ID', Entity Framework 错误

asp.net-mvc - 单元测试 Controller

asp.net-mvc - ASP.Net MVC 3 : Unit testing controller actions

asp.net-mvc-2 - ASP.NET MVC 2+ 和 PRG 模式

android - 我在android中的模型需要为单例吗?

asp.net-mvc - 该模型在MVC中的作用是什么?

ruby-on-rails - 如何为Mysql中的所有表创建模型rb文件

jquery - 如何在 View 文件夹中使用js文件

ruby-on-rails - 点击时渲染部分

asp.net-mvc - 使用模型和 ViewData 项创建 ViewDataDictionary 的简写?