0

I am trying to adapt the AccountController class so that it uses my own backend database. In order to do this completely I need to send a string to the _Layout.cshtml to be checked. How is this possible?

I have tried with ViewData but this requires the Controller to correspond to the view attached e.g. AccountController to View/Account/LogOn.cshtml will work.

I believe that ViewBag works in the same way as I am getting a null reference when I try to access it in the _Layout.cshtml.

At the moment my code is a bit damaged due to trying to fix the problem but here is what I have. Hopefully it will help to explain better.

AccountController/[HTTP-POST] LogOn

...
if (user.GetRole(model.UserName).Equals("Admin"))
{
     ViewBag.Role = "Admin";
}
...

_Layout.cshtml

@if (Request.IsAuthenticated && ViewBag.Role.Equals("Admin"))
{
   ...
}

I no longer think this can be done with ViewBag or ViewData (Due to comments). Any solution would be welcome.

Thank you in advance - Ankou

3
  • The code above does not work, it throws an exception basically saying that the ViewBag.Role cannot be referenced. I want to know how to send a string from one controller to a view it is not directly linked too. Commented Sep 13, 2011 at 14:22
  • Cannot be referenced, or cannot perform runtime binding on a null reference? If you are not setting the ViewBag.Role if the user is not an admin, then it will not exist Commented Sep 13, 2011 at 14:28
  • "Cannot perform runtime binding on a null reference" is the error I get. The backend database at the minute is just a tester one so I am sure that the user I am logging in with is flagged as being a Admin. Commented Sep 13, 2011 at 14:31

2 Answers 2

1

Change your code to

if (user.GetRole(model.UserName).Equals("Admin"))
{
    ViewBag.Role = "Admin";
}
else{
    ViewBag.Role = "";
}

You will get an error of ViewBag.Role does not exist. So it must always be set.

Edits

From your comments, I think you might be best creating a Child Action which has it's own controller and does the work for you.

@Html.RenderAction("LogonDisplay");

This will check the Roles, set the ViewBag values, and then display as needed.

Sign up to request clarification or add additional context in comments.

4 Comments

I really wish the answer had been this easy but its still giving me the error. I believe its due to Controllers having their own ViewBag and because AccountController isn't connected to _Layout view.
Ah so what you are saying is that in the code which the view is being returned, ViewBag.Role is not being set?
Yeah some how I need AccountController.ViewBag.Role instead of the HomeController.ViewBag.Role I think it must be accessing or another way of looking at it would be how do I send it to the HomeController.ViewBag
Thanks, I think I've taken up enough of your time. I'm just experimenting with trying to do the authentication in the HomeController but will try using a child action aswell. Thanks for the help, think I may have to leave this front end GUI stuff to the proffessionals and go back to doing back end stuff.
0

You may be using the _Layout before you call Logon action. Therefore, put a check on your _Layout.

@if (Request.IsAuthenticated && ViewBag.Role != null && ViewBag.Role.Equals("Admin"))
{
   ...
}

5 Comments

Thank you for your response. This does get rid of the error being thrown but just enforces the fact that the variable in the ViewBag is not getting passed. Im starting to think this just may not be possible. Thanks
Strange, viewbag should be there. Have you put a breakpoint in your action to verify the ViewBag.Role is being set?
Yes I have F11'ed through and the ViewBag.Role is definately set to "Admin"
I like @Tim B James approach as well, in his edit above. You may want to do that if you can't get passed this issue. I'm not sure how you're making ViewBag not work. Could you give more details on how your action is being called, you mentioned above trying to access the AccountControllers ViewBag instead of the HomeController's.
Thanks for the help. I think I will just try with a child action as I've used it before to display parts of the page so should be able to use it for this (maybe).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.