繁体   English   中英

Url.Action的一个对象路由值为null

[英]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正确传递,但userNamenull

看看由创建的网址

 @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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM