重定向时将数组或字符串列表从一个动作传递到另一个动作 [英] Passing an array or list of strings from one action to another when redirecting
问题描述
我有一个后期操作导入:
I have a post action Import:
public ActionResult Import()
{
var fileNames = new List<String>();
foreach (string path in Directory.GetFiles(directoryPath))
{
//do a whole bunch of stuff
...
fileNames.Add(path.Split('\\').Last());
}
return RedirectToAction("Index", new { InvalidFiles = fileNames });
}
如您所见,它重定向到Index
操作,并传递文件名List<String>
As you can see, it redirects to an Index
action, passing the List<String>
of file names
public ActionResult Index(List<String> InvalidFiles)
{
return View();
}
在Index
操作中,列表中包含正确数量的元素,但是所有实际字符串都已从文件名更改为字符串"System.Collections.Generic.List`1 [System.String]" .
In the Index
action, the List comes in with the right amount of elements, however all the actual strings has been changed from the filename to the string "System.Collections.Generic.List`1[System.String]".
知道为什么会这样吗?是否有更好的方法(可能使用TempData)将列表传递给新操作?
Any idea why this is happening? Is there a better way to pass the list to the new action, possibly using TempData?
推荐答案
You can use TempData to temporally hold the data between actions.
public ActionResult Import()
{
var fileNames = new List<String>();
foreach (string path in Directory.GetFiles(directoryPath))
{
//do a whole bunch of stuff
...
fileNames.Add(path.Split('\\').Last());
}
TempData["fileNames"] = fileNames;
return RedirectToAction("Index");
}
public ActionResult Index()
{
var invalidFiles = TempData["fileNames"] as List<String>;
return View();
}
这篇关于重定向时将数组或字符串列表从一个动作传递到另一个动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!