2011-03-03 76 views
3
public ActionResult DoSomething() 
{ 
return View("Index", new IndexModel { Foo = new List<string>() { "*" }); 
} 

其中Index.cshtml具有包含@Html.HiddenFor(m => m.Foo)存储在隐藏字段列表导致怪异的结果

public ActionResult ProcessForm(IndexModel model) 
{ 
} 

的ProcessForm里面你model.Foo表格包含一个字符串内容如下:

System.Collections.Generic.List`1[System.String] 

我很迷茫......

回答

5

这就是结果,如果你运行ToString()你的收藏,就像HiddenFor在做。你需要做一些特殊的事情来使列表成为一个字符串。

这里有一个快速和肮脏的LINQ语句,将其转换成一个逗号分隔的列表:

list.Aggregate("", (s,x) => string.IsNullOrEmpty(s) ? x : s + ", " + x); 
+1

我可能会转换成JSON,而不是,但你的答案不幸的是有道理.. *叹* – 2011-03-03 23:16:23

+2

为什么不只是使用string.Join(“,”,列表)? – Matt 2012-03-19 09:58:50

+1

已经过去了一年,但如果我没有记错的话,你不想传递null或空值。如果加入将这样做,我同意它会更简单。 – neontapir 2012-03-22 22:30:44