2011-03-03 73 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