关于如何在 MVC3 中完成特定功能的建议

发布于 2024-12-15 06:00:39 字数 1952 浏览 1 评论 0原文

我有一个 MVC3 应用程序,基于 VS 2010 的默认布局,我将其更改为如下图所示

Layout1

子菜单区域在 _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 });

从菜单启动的所有实体控制器均在控制器和视图中定义为标准意见。

我需要的是更改应用程序以使用如下布局

Layout2

当用户单击实体应用程序时应导航到 myapp/entities / 或 myapp/entities/index ,它应该在主工作区域中打开一个视图,如下所示

Layout3

然后当用户单击右侧子菜单,网址应类似于 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

Layout1

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

Layout2

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

Layout3

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一人独醉 2024-12-22 06:00:39

我设法(在某种程度上)使用以下方法完成了接近我需要的事情:

  1. 更改了_布局如下

    <节 id="main">
        
    @RenderBody()
    @RenderSection("EntityCRUD", false)
  2. 为实体创建了视图:

    @Html.Partial("_PanelEntitiesMenu")

  3. 将_PanelEntitiesMenu定义为

    实体

      @Html.Partial("_EntitiesMenu")
  4. 将实体视图(索引、编辑/删除/详细信息/创建)包含在

    <前><代码>@section EntityCRUD
    {
    @Html.Partial("_PanelEntitiesMenu")
    //...原始视图代码
    }

  5. 中,更改了所有涉及的视图,以将视图“正文”包含在部分中,并位于部分 I 的开头将面板菜单加载为部分视图

    <前><代码>@section EntityCRUD
    {
    @Html.Partial("_PanelEntitiesMenu")
    ....
    }

,这不完全是我想要的,但这是我发现的最好的迄今为止。

I managed (sort of) to accomplish something close to what I need using following approach:

  1. Changed the _layout as below

    <section id="main">
        <div>
            @RenderBody()
        </div>
        <div>
            @RenderSection("EntityCRUD", false)
        </div>
    </section>
    
  2. Created the view for Entities as:

    @Html.Partial("_PanelEntitiesMenu")

  3. Defined _PanelEntitiesMenu as

    <div id="sidebar">
    <h3>Entities</h3>
    <p></p>
        <ul>
        @Html.Partial("_EntitiesMenu")
        </ul>        
    </div> 
    
  4. Enclosing the entities views (Index, Edit / Delete / Details / Create) in

    @section EntityCRUD
    {
    @Html.Partial("_PanelEntitiesMenu")
    //... original view code
    }
    
  5. 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

    @section EntityCRUD
    {
        @Html.Partial("_PanelEntitiesMenu")
        ....
    }
    

Not exactly what I wanted, but that's the best I found so far.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文