通过列表< int>从动作链接到控制器方法 [英] Pass List<int> from actionlink to controller method

查看:112
本文介绍了通过列表< int>从动作链接到控制器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my controller I have this:

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

我想将该列表传递给另一个控制器..因此,我认为:

I want to pass that list to another controller.. so in my view I have:

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

控制器中的方法:

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

是否可以将整数列表作为路由值发送到控制器方法?

Is there a way to send a list of ints as a route value to a controller method?

推荐答案

不可能直接实现,但是如果我有List<int>

its not possible directly but you can do it with Json if i have 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>转换为json string

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>

这篇关于通过列表&lt; int&gt;从动作链接到控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆