Viewbag for all Asp.Net MVC C#controllers

Asked

Viewed 406 times

0

I have an application in ASP.Net MVC, after the user login need to show his name at the top of the page always, regardless of which page he is.

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

  1. Build a base controller by searching for the company name and returning it to a viewbag.

  2. I don’t remember well because I didn’t find the question again, but it was to use the pattern [discriminação] us controllers (if someone can explain to me how it is done).

Now a question I have about viewbag, if I use the first option and create a controller who returns to this viewbag, how will I use them on the pages? In case I just need to use on Shared\_Layout, right ? Someone could give me an example?

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

  • You are using what for authentication, Identity?

  • I didn’t do the login part, but I just asked a question about login and what I saw is not Identity, because it uses a redirect file called 'Starup.Auth.Cs' and doesn’t have this file in the project, i redirected it on web.config using the '<Authentication mode="Forms">' I don’t know if it helps .

  • You will use this value for some operation or just to show on screen?

  • Just show her on the screen

1 answer

1


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

html In the example we will use as _Name.cshtml it can be in the same folder of _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");

Now there are several other solutions without using viewbag.

  • So I was wondering what would be the most appropriate form for my case, for example, my menu already _Partial , but I call it as layout , and it is on this page that would be the information I need to share. It can be with or without viewbag, if it is with would like to know a little more about how to apply . If possible and clear , but thank you very much for your reply, I will try here

  • Could you give a slightly more detailed explanation of this custom controller? i have to create this controler and only assign the viewbag with the value I need ?

  • I will create a controller and a view for that controller, I will assign the value of viewbag to the controller and add it to my _Layout, @Partial_html(Novaview) right ? I’m a little hard to understand this logic, after using this partial in my layout I can already use the viewbag ?

  • Just a second, I’ll try to improve my explanation

  • Okay, I tried to clarify the code and how to do it. I changed the render function to see if it made it easier too, any doubts Tamo there

  • I couldn’t implode the way you told me, I built a control just with the Query and assigned the value I needed to Viewbag.Name and returned the view I created , I followed the example and put the name and in the layout call this _name ,with @Html.Patial ( renderAction didn’t work) but nothing works

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.