Search code examples
c#viewbagactionresult

c# ActionController returns ActionController Viewbag = empty


Just like the 1st question that i asked here. I am working with a group on a project for our scool.

We learned alot at this moment but we have just 1 issue

I Made a Logincontroller and in each Controller there is a call to the logincontroller to check if is logged in. its a little bit more complicated but thats what it does.

Like ActionResult List (Model model)

Session = Session["Login"];

Return LoginController.CheckedLoginAsAdmin(Session,model)

or without model return Logincontroller.checkedLoginAsAdmin(Session);

But for the Creates and Edits we use some Scafolded viewbags i gess... When we are using Viewbags -> they are set to NULL in the view. Because we RETURN an actionresult. Any id's to fix this issue ?

If you need more information, just ask it :-)

Thnx On Advance


Solution

  • Sounds like your basic approach is wrong. You should not be referencing the log in controller from other controllers. The log in controller should only be used when the user accesses the log in page, at which point the framework should create the controller for you.

    Once the user is logged in, you can get at the user object with User.Identity.Name which is part of ASP.NET. This is independent of any controller, and can be used wherever you need it.

    Maybe if you fix the way you are working, your other problems will go away. Do a search for "ASP.NET MVC authentication" and you'll find plenty of information on how you are supposed to do it.