在隐藏字段中存储列表会导致奇怪的结果

Sha*_*lle 3 hidden-field razor asp.net-mvc-3

public ActionResult DoSomething()
{
return View("Index", new IndexModel { Foo = new List<string>() { "*" });
}
Run Code Online (Sandbox Code Playgroud)

其中Index.cshtml有一个包含的表单 @Html.HiddenFor(m => m.Foo)

public ActionResult ProcessForm(IndexModel model)
{
}
Run Code Online (Sandbox Code Playgroud)

在ProcessForm中你的model.Foo包含一个字符串,其中包含:

System.Collections.Generic.List`1[System.String]
Run Code Online (Sandbox Code Playgroud)

我感到很困惑...

neo*_*pir 5

如果你运行ToString()你的收藏,那就是结果,就像HiddenFor这样做.您需要做一些特殊的事情才能将列表变成字符串.

这是一个快速而又脏的Linq语句,它将其转换为以逗号分隔的列表:

list.Aggregate("", (s,x) => string.IsNullOrEmpty(s) ? x : s + ", " + x);
Run Code Online (Sandbox Code Playgroud)

  • 为什么不使用string.Join(",",list)? (2认同)