Is there are way to retrieve form fields with the same name besides using modelbinders or comma splitting.
除了使用modelbinders或逗号分割之外,是否还有方法可以检索具有相同名称的表单字段。
I have a few textfields with the same name and i need to loop through them and retrieve each value.
我有一些具有相同名称的文本字段,我需要遍历它们并检索每个值。
Thank you
2 个解决方案
#1
FormCollection is a NameValueCollection. That means you can do:
FormCollection是一个NameValueCollection。这意味着你可以这样做:
public ActionResult MyAction(FormCollection form)
{
// ModelBinder will set "form" appropriately
foreach(var value in form.Getvalues("duplicatedFieldname"))
{
//do something with value
}
}
#2
Even easier:
public ActionResult MyMethod(string[] fieldName)
Or use List<string>
if you prefer that instead of string[]
.
或者如果您喜欢使用List
#1
FormCollection is a NameValueCollection. That means you can do:
FormCollection是一个NameValueCollection。这意味着你可以这样做:
public ActionResult MyAction(FormCollection form)
{
// ModelBinder will set "form" appropriately
foreach(var value in form.Getvalues("duplicatedFieldname"))
{
//do something with value
}
}
#2
Even easier:
public ActionResult MyMethod(string[] fieldName)
Or use List<string>
if you prefer that instead of string[]
.
或者如果您喜欢使用List