How to do this using @RenderBody from Asp.net MVC?

7

All screens in my system are Views and are rendered in the @RenderBody in the main _Layout

I'd like to change my layout and make it look like this:

So basically it's how I explained it in the image, Click the Menu will render there, and after clicking on the submenu side render again there, without removing the menu

What I thought about creating a partial with the menu, but then I would have to add it to all my Views ...

PartialMenuCadastro - > Menu items ...

Then put them in the client list, creation, editing, etc, etc. in the dashboard ...

I do not know if this is correct, and it does not look very elegant to me ...

I also thought about rendering using Ajax, but then I would have to change all my views to partial and I do not know if that would be correct either ...

Does anyone have an idea for me?

    
asked by anonymous 09.09.2014 / 16:19

2 answers

10

A solution to use Ajax , without having to change all% from Views to PartialViews , would be to Ajax request on Action and then only get RenderBody generated from View .

An example to get clearer:

_Layout

(...)
<div id="Conteudo">
    @RenderBody()
</div>
(...)

Then on your View :

<a onclick="Open('Modulo/Index')">Clique aqui</a>
<div id="Conteudo2">
</div>

<script>
    function Open(url) {
        url = '@Url.Content("~/")' + url;
        $.ajax({
            url: url,
            type: 'GET',
            success: function (response) {
                $('#Conteudo2').html($(response).find('#Conteudo'));
            },
            error: function () {
                alert('Ocorreu um erro!');
            }
        });
    }
</script>
    
12.09.2014 / 22:41
0

I find AJAX uninteresting for this solution.

It seems to me that the content of this side menu is directly related to the access link in the top menu. You can pass this list of side menu links dynamically (by ViewBag or something like that) and render it normally in _Layout.

On _Layout:

<div class="conteudo">
    <div class="menulateral">
        @foreach(var item in ViewBag.LinksFromMenuSuperior) {
            <a href="@item.Link">@item.Titulo</a>
        }
    </div>
    <div>
        @RenderBody()
    </div>
</div>

You can also use @Section {} and @RenderSection (), which you may not know because you did not comment as an option, but worth a query, unfortunately it (in this solution) would behave much like @PartialView () because you will also have to change the pages.

    
15.09.2014 / 06:29