我正在尝试将两个参数传递给控制器,以便返回特定应用程序中当前用户的角色列表。然后,此列表用于自动填充搜索框。
我的问题出现了,因为我的Url.Action助手中的一个对象路由变量始终为null。代码位于部分视图的脚本中,如下所示:
<script type="text/javascript">
function roleFilter(element) {
element.kendoAutoComplete({
dataSource: {
transport: {
read: "@Url.Action("FilterMenuCustomization_Roles", new { userName = ViewBag.UserName, applicationName = ViewBag.ApplicationName })"
}
}
});
}
</script>
这部分代码是失败的并给出一个空值:
applicationName = ViewBag.ApplicationName
我知道ViewBag包含正确的值,但在此脚本中它是空的,但我可以在页面上的其他地方的代码中传递它。如果任何人可以帮助它将非常感激。
完整部分视图:
@(Html.Kendo().Grid<BUUK.BSS.Models.Role>()
.Name("grid")
.DataSource(dataSource => dataSource // Configure the grid data source
.Ajax() // Specify that ajax binding is used
.Read(read => read.Action("CurrentUserRoles_Read", "User",
new
{
userName = ViewBag.UserName,
applicationName = ViewBag.ApplicationName
})) // Set the action method which will return the data in JSON format
.PageSize(15)
)
.Columns(columns =>
{
columns.Bound(role => role.RoleName)
.Filterable(filterable => filterable.UI("roleFilter"));
})
.Pageable() // Enable paging
.Sortable() // Enable sorting
.Filterable(filterable => filterable
.Extra(false)
.Messages(m => m.Info("Items with value equal to:")
)
).Events(e => e.FilterMenuInit("filterMenuInit"))
)
<p style="margin-top:5px;">
@Html.ActionLink("Add Roles", "EditRoles", "User", new { applicationName = ViewBag.ApplicationName, userName = ViewBag.UserName }, null)
</p>
<script type="text/javascript">
function roleFilter(element) {
element.kendoAutoComplete({
dataSource: {
transport: {
read: "@Url.Action("FilterMenuCustomization_Roles", new { userName = ViewBag.UserName, applicationName = ViewBag.ApplicationName })"
}
}
});
}
</script>
控制器:
public ActionResult Roles(string userName, string applicationName, string status)
{
var model = new List<Role>();
foreach (KeyValuePair<Application.Applications, IRole> entry in Application.RoleMap)
{
if (entry.Key.ToString() == applicationName)
{
IRole role = entry.Value;
model = role.GetUserRoles(userName);
}
}
ViewBag.ApplicationName = applicationName;
ViewBag.UserName = userName;
ViewBag.Status = status;
return View(model);
}
public ActionResult FilterMenuCustomization_Roles(string userName, string applicationName)
{
var model = new List<Role>();
foreach (KeyValuePair<Application.Applications, IRole> entry in Application.RoleMap)
{
if (entry.Key.ToString() == applicationName)
{
IRole role = entry.Value;
model = role.GetUserRoles(userName);
}
}
return Json(model.Select(e => e.RoleName).Distinct(), JsonRequestBehavior.AllowGet);
}
我很乐意提供所需的任何其他信息,再次感谢您的帮助。
修改
如果我改变参数的写入顺序,即
read: "@Url.Action("FilterMenuCustomization_Roles", new { applicationName = ViewBag.ApplicationName, userName = ViewBag.UserName })"
而不是如上所述。 applicationName
正确传递,但userName
为null
而不是
答案 0 :(得分:3)
查看由
创建的网址 @Url.Action("FilterMenuCustomization_Roles", new { userName = ViewBag.UserName, applicationName = ViewBag.ApplicationName })
在你的JavaScript中。它可能格式不正确。尝试:
"@Html.Raw(Url.Action("FilterMenuCustomization_Roles", new { userName = ViewBag.UserName, applicationName = ViewBag.ApplicationName }))"