2010-01-06 102 views
2

比方說,我有場名爲Customers如何綁定逗號分隔值的列表列出<...>

<input type="text" name="Customers" 

,我想在它進入逗號分隔的我的客戶ID,然後接受它在ASP.NET MVC端作爲List。這個功能是否構建在ASP.NET MVC中,如果不是,最好的方法是什麼?

回答

5

製作一個逗號分隔的字符串列表:

var myList = mytextbox.text.Split(',').ToList(); 
1

你必須給你的輸入類型一個ID,以便你可以在你的代碼後面訪問它。

然後,你可以做這樣的事情:

List<string> mylist = new List<string>(); 
string[] mystrings; 
string text = mytextbox.text; 

mystring = text.Split(','); 

foreach (string str in mystrings) 
    mylist.Add(str); 
+0

-1 Web窗體 – 2011-11-03 11:53:25

5

你既可以有不分裂(如彼得提到一個模型綁定),或者您可以使用JavaScript爲所有值添加具有相同名稱的隱藏字段。喜歡的東西:

<input type="hidden" name="customers" value="102" /> 
<input type="hidden" name="customers" value="123" /> 
<input type="hidden" name="customers" value="187" /> 
<input type="hidden" name="customers" value="298" /> 
<input type="hidden" name="customers" value="456" /> 

那麼你的行動將採取如int的枚舉:

public ActionResult DoSomethingWithCustomers(IEnumerable<int> customers){....}