0

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

Community
  • 1
  • 1
bzamfir
  • 4,698
  • 10
  • 54
  • 89

2 Answers2

1

Are you talking about @RenderSection http://blogs.msdn.com/b/marcinon/archive/2010/12/08/optional-razor-sections-with-default-content.aspx

Jamie R Rytlewski
  • 1,172
  • 9
  • 22
  • Not quite, because @RenderSection can be only in main view, and I wanted to have it in Entities Index view. I managed to "hack" it (sort of) to get something close to what I need, but it required to change ALL views, first to "wrap" the view code in @section{..} and then to load the Entities menu view as partial. Not a good solutio because it involves changing lot of files and the design is not clean. Any other idea? – bzamfir Nov 12 '11 at 02:45
0

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.

bzamfir
  • 4,698
  • 10
  • 54
  • 89