将 List 从 actionlink 传递到控制器方法

2024-01-13

在我的控制器中我有这个:

ViewBag.lstIWantToSend= lstApps.Select(x => x.ID).ToList();  // creates a List<int> and is being populated correctly

我想将该列表传递给另一个控制器..所以在我看来我有:

@Html.ActionLink(count, "ActionName", new { lstApps = ViewBag.lstIWantToSend }, null)

控制器中的方法:

public ActionResult ActionName(List<int> lstApps)   // lstApps is always null

有没有办法将整数列表作为路由值发送到控制器方法?


它不可能直接完成,但你可以这样做Json如果我有List<int>

ViewBag.lstIWantToSend= new List<int> {1, 2, 3, 4};

所以我的观点是这样的

@Html.ActionLink(count, "ActionName", new { lstApps = Json.Encode(ViewBag.lstIWantToSend) }, null)

Json.Encode将转换List<int> to json string

and ActionName会是这样

 public ActionResult ActionName (string lstApps)
        {
            List<int> result = System.Web.Helpers.Json.Decode<List<int>>(lstApps);

            return View();

        }

Json.Decode<List<int>>将转换这个json string回到List<int>

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 List 从 actionlink 传递到控制器方法 的相关文章

随机推荐