如何修复传递的列表中的参数值是0

时间:2019-02-02 15:59:48

标签: c# asp.net-mvc list

我想值的列表传递到另一个方法但是一旦第二个方法被调用列表中失去了它的价值。

第一种方法:

public ActionResult Create_Filter([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<CourseFilterViewModel> courseFilterVM)
    {
var results = new List<Results>
        {
            new Results {CourseNumber = "100", CourseTitle = "Test Title", CourseSubject = "Subject Test"}
        };

        return RedirectToAction("TestView", "Filter", new {results});

}

第二种方法:

public ActionResult TestView(List<Results> results)
    {
        //results value here is 0               

        return null;
    }

我期望的输出:CourseNumber = “100”,CourseTitle = “测试标题”,CourseSubject = “主题测试”)

2 个答案:

答案 0 :(得分:1)

当您执行重定向到操作时,参数作为查询字符串传递,因此您不能传递整个对象,请参见以下问题: passing object in redirecttoaction

答案 1 :(得分:0)

尝试使用TempData。数据将转到一个重定向。

public ActionResult Create_Filter([DataSourceRequest] DataSourceRequest request, 
[Bind(Prefix = "models")]IEnumerable<CourseFilterViewModel> courseFilterVM)
{
    var results = new List<Results>
    {
        new Results {CourseNumber = "100", CourseTitle = "Test Title", 
         CourseSubject = "Subject Test"}
    };
    TempData["results"] = results;
    return RedirectToAction("TestView", "Filter");

 }
 public ActionResult TestView()
 {

    if(TempData["results"] != null) {
      //do something
    }
    return whatever;
 }