0

I've had a MVC 4 / Entity web project dropped into my lap, and I've never used either before. I'm getting through it but the simple things are really tripping me up - Like hiding or displaying a link in my layout based on a parameter in the database.

I created a function in my HomeController that simply sets 2 bools in the ViewBag for whether or not a person is a manager or superuser. I call that function using

@Html.Action("SetupViewBag", "Home")

which sits right after the <body> tag in my layout. Here is the code for SetupViewBag:

public void SetupViewBag()
{
   ViewBag.IsManager = ADAccess.IsManager(SessionManager.GetUserName());
   ViewBag.IsSuper = SessionManager.SuperUser();
}

The bools are set properly and in the right order when I set up break points, but when I try to access them using the below code, I get a 'Cannot convert null to 'bool' because it is a non-nullable value type.'

@{
   if((bool)@ViewBag.IsManager){                       
      <li>@Html.ActionLink("Management", "Management", "Home",null, new { id = "managementLink" })</li>
   }                     
}

There has to be something really simple I'm missing. Any help is greatly appreciated.

2
  • When you say "bools are set properly" you mean you have ViewBag.IsManager=true (or false) in your code? And that you do not have it inside an if-else that would make it null on the view? Commented Mar 26, 2013 at 6:31
  • Updated question with the SetupViewBag code. When I set a break point inside SetupViewBag and on the if((bool)@ViewBag.IsManager){ line, the ViewBag.IsManager is set before it is referenced. However, it is referenced in the layout file, not a view per-se, which I believe to be my problem after ongoing research. Commented Mar 26, 2013 at 6:36

2 Answers 2

2

Based on your comment, @Dakine83, you should setup your ViewBag on the controller constructor method like so:

public class YourController : BaseController {
    public YourController(){

    }
}

The reason for that is because the Layout page is already rendered the time your action method has been called. The reason you have a null ViewBag.IsManager.

UPDATE: Use a base controller

public class BaseController : Controller {
    public BaseController() {
            ViewBag.IsManager = ADAccess.IsManager(SessionManager.GetUserName());
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

That's what I was afraid of. Is the only option to have the exact same function on each of my 8 controllers? That seems ridiculous, but not surprising.
No, you can have, and I suggest "should have", a base controller. See my updated answer.
1

i hope this might work for you,please try it once @Html.ActionLink("Management", "Management", "Home", new { id = false }, null);

Thanks

3 Comments

The link works just fine - it's the check to see if the link should be displayed that is breaking.
I get a 'Cannot convert null to 'bool' because it is a non-nullable value type i think you have mention above issue, and it will be resolved if you set new { id = true/false } as the parameter in the actin methos is of bool type.
The 'cannot convert' is not coming from that line. That line works as expected without the if around it.

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.