bzamfir
bzamfir

Reputation: 4906

Suggestions on how to accomplish a specific functionality in MVC3

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

Upvotes: 0

Views: 120

Answers (2)

bzamfir
bzamfir

Reputation: 4906

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.

Upvotes: 0

Related Questions