Viewbag for all controllers Asp.Net MVC C #

0

I have an application in ASP.Net MVC, after the user logs in I need to show their name at the top of the page, regardless of what page it is.

This application is an administrative where the name of the company will be at the top, I saw some solutions to my problem but I can not decide which would be better, because I have little knowledge.

  • Build a base controller by searching the name of the company and returning it to a viewbag .

  • Now I have a question about viewbag , if I use the first option and create a controller that returns to that viewbag use them on pages? In case I need only use in [discriminação] , right? Could someone give me an example?

    I have little idea about this part which is hindering my learning, I would like a slightly more didactic answer about the operation of even viewbag , I found other answers on this subject but I could not understand them completely.

        
    asked by anonymous 26.09.2016 / 21:22

    1 answer

    1

    One way to solve your problem using viewbags would be to use partial view , so you could put a custom controller by setting the viewbag and just give @Html.RenderAction on the shared layout page of your system. In this way we would have:

    html In the example we will use as _Name.cshtml it can be in the same folder as _Layout.cshtml

    <div>
        @Viewbag.nome
    </div>
    

    controller In the example we will put as ActionController.cs

    public PartialViewResult Action(){
        Viewbag.nome = "nome";
        return PartialView("~/views/Shared/_Nome.cshtml");
    
    }
    

    _layout

    @Html.RenderAction("Action", "Action");
    

    There are now several other solutions without using viewbag .

        
    26.09.2016 / 21:35