4

我在同一个控制器中有 2 个动作。

public ActionResult Index(string filter, int[] checkedRecords)

public ActionResult ExportChkedCSV(string filter, int[] checkedRecords)

第二个操作 (ExportChkedCSV) 包含此重定向:

if (reject != 0)
        {
            return RedirectToAction("Index", new { filter, checkedRecords });
        }

当我逐步完成时,在 RedirectToAction 语句中正确填充了参数 checkedRecords,但是当它从那里点击 Index ActionResult 时,checkedRecords 为空。我尝试过 filter =、checkedRecords = 等。从 View 到 Controller,我对此没有任何问题。如果我将数组类型更改为其他类型,我可以获取该值 - 如何将 int[] 从一个动作传递到另一个动作?我究竟做错了什么?谢谢

4

3 回答 3

6

您不能在 MVC 中将复杂类型作为重定向参数发送,只能发送像数字和字符串这样的原始类型

使用 TempData 传递数组

...
if (reject != 0) {
    TempData["CheckedRecords"] = yourArray;
    return RedirectToAction("Index", new { filter = filterValue });
}
...

public ActionResult Index(string filter) {
    int[] newArrayVariable;
    if(TempData["CheckedRecords"] != null) {
        newArrayVariable = (int[])TempData["CheckedRecords"];
    }
    //rest of your code here
}
于 2012-05-21T01:09:27.317 回答
1

您正在发送两个空值。当您使用 new {} 时,您正在创建一个新对象。您不仅要定义索引名称,还要定义值。

return RedirectToAction("Index", new { filter = filter, checkedRecords = checkedRecords });
于 2012-05-20T23:45:31.167 回答
1

我不确定 ASP.Net 是否知道如何使用您传递的 int 数组构建 URL。如果 int 数组唯一标识一个资源,那么您可以尝试将数组转换为连字符分隔(或类似)的字符串,然后在 Index 方法中解析该字符串。

如果您只是想在请求之间保留数据,请使用 TempData:

http://msdn.microsoft.com/en-us/library/system.web.mvc.controllerbase.tempdata.aspx

于 2012-05-21T00:35:24.083 回答