关于如何在 MVC3 中完成特定功能的建议
我有一个 MVC3 应用程序,基于 VS 2010 的默认布局,我将其更改为如下图所示
子菜单区域在 _layout.cshtml 中定义为,
<div id="sidebar">
<h3>Entities</h3>
<p></p>
<ul>
@Html.Partial("_EntitiesMenu")
</ul>
</div>
<section id="main">
@RenderBody()
</section>
并且 _EntitiesMenu 包含如下条目
<li>@Html.ActionLink("Addresses", "Index", "Address")</li>
<li>@Html.ActionLink("Applications", "Index", "Application")</li>
我有一个 MapRoute 定义为
routes.MapRoute("Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
{ controller = "Home", action = "Index", id = UrlParameter.Optional });
从菜单启动的所有实体控制器均在控制器和视图中定义为标准意见。
我需要的是更改应用程序以使用如下布局
当用户单击实体应用程序时应导航到 myapp/entities / 或 myapp/entities/index ,它应该在主工作区域中打开一个视图,如下所示
然后当用户单击右侧子菜单,网址应类似于 myapp/entities/entity1/index、myapp/entities/entity1/edit/1 等(与现在完全相同,但在实体页面“下方”。
我定义了实体控制器
public class EntitiesController : Controller
{
public ActionResult Index()
{ return View();}
}
,它的视图看起来像
<div id="workarea">
// here should became new Body region, to load all views called from the other controllers
// something like @RenderBody(), but this don't works
</div>
<div id="sidebar">
<h3>Entities</h3>
<ul>
@Html.Partial("_EntitiesMenu")
</ul>
</div>
我不想对实体控制器或视图进行更改(或者如果绝对必要,则进行最小的更改,因为它们有很多)。我可以在实体范围内将该区域指定为主体吗?如果用户单击顶部主页/关于,它会从 _layout.cshtml“卸载”EntitiesView 吗?
不确定我的问题是否很清楚,但我希望有人能理解我的意思。
谢谢
I have a MVC3 application, based on default layout from VS 2010, which I changed to looklike in image below
The submenu area is defined in _layout.cshtml as
<div id="sidebar">
<h3>Entities</h3>
<p></p>
<ul>
@Html.Partial("_EntitiesMenu")
</ul>
</div>
<section id="main">
@RenderBody()
</section>
and the _EntitiesMenu contains entries as below
<li>@Html.ActionLink("Addresses", "Index", "Address")</li>
<li>@Html.ActionLink("Applications", "Index", "Application")</li>
I have a single MapRoute defined as
routes.MapRoute("Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
{ controller = "Home", action = "Index", id = UrlParameter.Optional });
All my entity controllers started from menu are defined standard in Controllers and views in Views.
What I need is to change the app to use a layout as below
When users click Entities app should navigate to myapp/entities/ or myapp/entities/index and it should open a view in main work area that will look like below
Then when users click on right submenus, the url should look like myapp/entities/entity1/index, myapp/entities/entity1/edit/1, etc (exactly like now but "under" entities page.
I defined the Entities controller like
public class EntitiesController : Controller
{
public ActionResult Index()
{ return View();}
}
And it's view looks like
<div id="workarea">
// here should became new Body region, to load all views called from the other controllers
// something like @RenderBody(), but this don't works
</div>
<div id="sidebar">
<h3>Entities</h3>
<ul>
@Html.Partial("_EntitiesMenu")
</ul>
</div>
I don't want to have to make changes to entities controllers or views (or minimal changes if absolutely necessary, because there are lot of them). Can I somehow assign that area as main Body, while under scope of entities? And if user click on top Home / About, it will "unload" the EntitiesView from _layout.cshtml?
Not sure if my question is very clear, but I hope someone will understand what I'm after.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你在谈论 @RenderSection http://blogs.msdn.com/b/marcinon/archive/2010/12/08/optional-razor-sections-with-default-content.aspx
Are you talking about @RenderSection http://blogs.msdn.com/b/marcinon/archive/2010/12/08/optional-razor-sections-with-default-content.aspx
我设法(在某种程度上)使用以下方法完成了接近我需要的事情:
更改了_布局如下
为实体创建了视图:
@Html.Partial("_PanelEntitiesMenu")
将_PanelEntitiesMenu定义为
将实体视图(索引、编辑/删除/详细信息/创建)包含在
中
<前><代码>@section EntityCRUD
{
@Html.Partial("_PanelEntitiesMenu")
//...原始视图代码
}
中,更改了所有涉及的视图,以将视图“正文”包含在部分中,并位于部分 I 的开头将面板菜单加载为部分视图
<前><代码>@section EntityCRUD
{
@Html.Partial("_PanelEntitiesMenu")
....
}
,这不完全是我想要的,但这是我发现的最好的迄今为止。
I managed (sort of) to accomplish something close to what I need using following approach:
Changed the _layout as below
Created the view for Entities as:
@Html.Partial("_PanelEntitiesMenu")
Defined _PanelEntitiesMenu as
Enclosing the entities views (Index, Edit / Delete / Details / Create) in
Changed all involved views to have the view "body" included in section, and at the beginning of the section I load the panel menu as partial view
Not exactly what I wanted, but that's the best I found so far.