如何绑定逗号分隔值的列表列出<...>

问题描述:

比方说,我有场名为Customers如何绑定逗号分隔值的列表列出<...>

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

,我想在它进入逗号分隔的我的客户ID,然后接受它在ASP.NET MVC端作为List。这个功能是否构建在ASP.NET MVC中,如果不是,最好的方法是什么?

制作一个逗号分隔的字符串列表:

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

你必须给你的输入类型一个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

你既可以有不分裂(如彼得提到一个模型绑定),或者您可以使用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){....}