【发布时间】:2010-01-06 10:18:36
【问题描述】:
假设我有一个名为“客户”的字段
<input type="text" name="Customers"
我想在其中输入我的客户的逗号分隔 ID,然后在 ASP.NET MVC 端作为列表接收它。此功能是否在 ASP.NET MVC 中构建,如果不是,最好的方法是什么?
【问题讨论】:
标签: asp.net-mvc
假设我有一个名为“客户”的字段
<input type="text" name="Customers"
我想在其中输入我的客户的逗号分隔 ID,然后在 ASP.NET MVC 端作为列表接收它。此功能是否在 ASP.NET MVC 中构建,如果不是,最好的方法是什么?
【问题讨论】:
标签: asp.net-mvc
制作一个逗号分隔的字符串列表:
var myList = mytextbox.text.Split(',').ToList();
【讨论】:
您可以拥有一个执行拆分的模型绑定器(就像 Peter 提到的那样),或者您可以使用 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){....}
【讨论】:
您必须为您的输入类型提供一个 id,以便您可以在后面的代码中访问它。
然后你可以这样做:
List<string> mylist = new List<string>();
string[] mystrings;
string text = mytextbox.text;
mystring = text.Split(',');
foreach (string str in mystrings)
mylist.Add(str);
【讨论】: