在加载页面之前,我需要检查用户的安全级别。这将确定他们能够看到导航栏的哪些元素。我在$(document).ready
函数内部尝试了两种运行此ajax调用的方法。
(名为div
的{{1}}包含以下html中的container
ul
由于这是一个ASP MVC 3站点,我在$(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")'
});
};
});
方法中设置了一个断点。我可以说,由于它从未被解雇,因此呼叫无效。
控制器方法如下所列
CheckSecurity
这应该检查用户的安全级别,将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();
}
值放在ViewBag中,然后确定是否显示下面的boolean
下拉项
Admin Features
当页面尝试加载时,它会因为指向<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>
行的错误而崩溃:
@if (ViewBag.Admin)
任何帮助/建议非常感谢。 THX!
答案 0 :(得分:0)
/ ViewBags只能在同一个视图中使用。这意味着:
如果你在
public ActionResult Index()
{
ViewBag.Index
}
然后由ajax执行
public ActionResult OtherAction()
{
ViewBag.Other
}
ViewBag.Other
在索引
public ActionResult Index()
{
ViewBag.Index
ViewBag.Other//erro other is null cuz because bellows to OtherAction view
}
因此,您可以返回一个列表。