0

Before the page loads, I need to run a check on the user's security level. This will determine which elements of our nav bar they are able to see. I have tried two methods at running this ajax call, both inside the $(document).ready function.

(the div named container encompasses the ul in the html below)

$(document).ready(function ($) {
    //First attempt
    $('#container').load(
        $.ajax({
            type: 'GET',
            url: '@Url.Action("CheckSecurity","Home")'
        })
    );

    //Second attempt 
    window.onload = function () {
        $.ajax({
            type: 'GET',
            url: '@Url.Action("CheckSecurity","Home")'
        });
    };
});

Since this is an ASP MVC 3 site, I've set a breakpoint in the CheckSecurity method. I can tell neither call is working due to the fact it is never fired.

The controller method is listed below

public ActionResult CheckSecurity()
{
    ViewBag.UserName = Security.GetUserName(User);
    ViewBag.Admin = Security.IsAdmin(User);
    ViewBag.ITsupport = Security.IsItSupport(User);
    ViewBag.Read = Security.IsViewer(User);
    ViewBag.Modify = Security.IsModifier(User);
    return View();            
}

This is supposed to check the user's security level, place the boolean value in the ViewBag, and then determine whether or not to display the Admin Features drop down item below

<li class="dropdown">  
    <a href="#"  
       class="dropdown-toggle"  
       data-toggle="dropdown"
       data-hover="dropdown">  
        Admin  
        <b class="caret"></b>  
    </a>  
    <ul class="dropdown-menu">  
        <li>@Html.MenuLink("Products", "Index", "Product")</li>
        <li>@Html.MenuLink("Product Training", "Index", "Course")</li>
        <li>@Html.MenuLink("Continuing Ed", "Index", "ContEdCourse")</li>
        @if (ViewBag.Admin)
        {
            <li>@Html.MenuLink("Admin Features", "Index", "DropDownValues")</li>             
        }
    </ul>  
</li>

When the page attempts to load, it instead crashes with this error pointing to the @if (ViewBag.Admin) line:

Cannot convert null to 'bool' because it is a non-nullable value type

Any help/suggestions greatly appreciated. Thx!

Charlino
  • 15,802
  • 3
  • 58
  • 74
NealR
  • 10,189
  • 61
  • 159
  • 299
  • Do you have an element with an `id="container"`? and does that element even have a load event? (only iframes images scripts and the window have load events). the second attempt should have worked though. – Kevin B May 13 '13 at 18:24

1 Answers1

0

/ViewBags just work in the same View. Which means this:

If you are in

public ActionResult Index()
{
    ViewBag.Index
}

and then you execute by ajax

public ActionResult OtherAction()
{
    ViewBag.Other
}

The ViewBag.Other will not be visible in Index

public ActionResult Index()
{
    ViewBag.Index
    ViewBag.Other//erro other is null cuz because bellows to  OtherAction view
}

So, you could return a list instead.

Ligth
  • 330
  • 1
  • 9
  • So basically whatever controller/method you are working in is the one that needs to set the `ViewBag`? – NealR May 13 '13 at 16:24
  • I'm still curious why the Ajax isn't being fired off though. Any ideas on that? – NealR May 13 '13 at 16:26
  • i don't see any html where you have an element with id of `container`. Are you sure the selector `$(#container)` selects any elements? – Mike Corcoran May 13 '13 at 16:31
  • Make sure that the CheckSecurity action result is in HomeController also. And see this post, that may be causing the problem http://stackoverflow.com/questions/11767911/mvc-httppost-httpget-for-action – Ligth May 13 '13 at 16:31
  • It is in the `HomeController`. What's odd is, since this is a shared layout page, the Ajax actually does fire when you load the site's home/index.cshtml page. However, navigate to anywhere else in the site (all pages use the same layout w/the navbar/ajax code) and the method will not get called. – NealR May 13 '13 at 16:42