[英]@Url.Action returns a null value within the jQuery .load() method
[英]Url.Action has one object route value as null
我試圖將兩個參數傳遞給控制器,以返回特定應用程序中當前用戶的角色列表。 然后,此列表用於自動完成搜索框。
出現我的問題是因為對象路由變量之一在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
看看由創建的網址
@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 }))"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.