asp.net mvc - Make Ajax call before page loads -
before page loads, need run check on user's security level. determine elements of our nav bar able see. have tried 2 methods @ running ajax call, both inside $(document).ready
function.
(the div
named container
encompasses ul
in 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 asp mvc 3 site, i've set breakpoint in checksecurity
method. can tell neither call working due fact never fired.
the controller method 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 supposed check user's security level, place boolean
value in viewbag, , determine whether or not display 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 page attempts load, instead crashes error pointing @if (viewbag.admin)
line:
cannot convert null 'bool' because non-nullable value type
any help/suggestions appreciated. thx!
/viewbags work in same view. means this:
if in
public actionresult index() { viewbag.index }
and execute ajax
public actionresult otheraction() { viewbag.other }
the viewbag.other
not visible in index
public actionresult index() { viewbag.index viewbag.other//erro other null cuz because bellows otheraction view }
so, return list instead.
Comments
Post a Comment