I have a form which allows the user to create extra "rows" using JQuery (using .clone) so that they can decide how many of the same information they need to submit. My issue is that I cannot work out how to access these form items within my controller.
我有一个表单,允许用户使用JQuery(使用.clone)创建额外的“行”,以便他们可以决定需要提交多少相同的信息。我的问题是我无法弄清楚如何在我的控制器中访问这些表单项。
the form that is being submitted may look like this
提交的表单可能如下所示
<input type="text" name="Amount" id="Amount">
<select name="Item">
<option value="1">Item 1"</option>
<option value="2">Item 2"</option>
<option value="3">Item 3"</option>
</select>
<input type="text" name="Amount" id="Amount">
<select name="Item">
<option value="1">Item 1"</option>
<option value="2">Item 2"</option>
<option value="3">Item 3"</option>
</select>
<input type="text" name="Amount" id="Amount">
<select name="Item">
<option value="1">Item 1"</option>
<option value="2">Item 2"</option>
<option value="3">Item 3"</option>
</select>
Basically, the block between input
and the select could be repeated an infinite number of times. When I submit to the controller I am then using FormCollection form
to access the form elements. from there I am unsure how I can access the items that have been submitted. I thought of using a for loop and then accessing them via something like form["Amount"][i] but obviously that is not going to work.
基本上,输入和选择之间的块可以重复无限次。当我提交给控制器时,我正在使用FormCollection表单来访问表单元素。从那里我不确定如何访问已提交的项目。我想过使用for循环,然后通过类似[[Amount]] [i]之类的东西来访问它们,但显然这不起作用。
Am I going about this the right way and if so, does anyone have any suggestions about how this might work?
我是否以正确的方式解决这个问题?如果有的话,是否有人对这可行的方法有任何建议?
Thanks in advance.
提前致谢。
6 个解决方案
#1
12
Old question, but still... You can get the posted values as an array by calling Request.Form.GetValues
, or Request.QueryString.GetValues
. For example:
旧问题,但仍然......您可以通过调用Request.Form.GetValues或Request.QueryString.GetValues将发布的值作为数组获取。例如:
string[] amounts = Request.Form.GetValues("Amount");
And the amounts
array will contain the correct values, so you can post values containing comas, dots, whatever, and don't worry about splitting/parsing it.
amount数组将包含正确的值,因此您可以发布包含comas,dots等的值,并且不用担心拆分/解析它。
Of course if you are running MVC, use the modelbinder to do it. But you can use this if you are using webforms, a generic handler, etc...
当然,如果您正在运行MVC,请使用modelbinder来执行此操作。但是如果你使用webforms,通用处理程序等,你可以使用它...
#2
6
Check out Model Binding To A List. Your Action method should be:
查看模型绑定到列表。你的Action方法应该是:
public ActionResult MyAction(string[] Amount, int[] Item){
// ...
}
However this will make you need to "link" the items. Alternatively create a "Item" class:
但是,这将使您需要“链接”项目。或者创建一个“Item”类:
public class Item {
public string Amount { get; set; }
public int Item { get; set; }
}
And
和
public ActionResult MyAction(IList<Item> items){
// ...
}
And your markup should be:
你的标记应该是:
<input type="hidden" name="items.Index" value="0" />
<input type="text" name="items[0].Amount" id="items[0].Amount">
<select name="items[0].Item">
<option value="1">Item 1"</option>
<option value="2">Item 2"</option>
<option value="3">Item 3"</option>
</select>
<input type="hidden" name="items.Index" value="1" />
<input type="text" name="items[1].Amount" id="items[1].Amount">
<select name="items[1].Item">
<option value="1">Item 1"</option>
<option value="2">Item 2"</option>
<option value="3">Item 3"</option>
</select>
Etc...
等等...
#3
4
I believe if you have multiple fields named Amount the values will be comma delimited.
我相信如果您有多个名为Amount的字段,则值将以逗号分隔。
To access each one just try:
要访问每个只是尝试:
string[] amounts = Request.Form["Amount"].Split(new char[] { ',' });
Keep in mind though, the inputs are not cleaned on submission so if someone enters a comma into the text box it's going to cause issues.
但请记住,输入不会在提交时清除,因此如果有人在文本框中输入逗号,则会导致问题。
Hence I'd recommend numbering them.
因此我建议给它们编号。
#4
1
I ended up realising that (blush) the mechanism which JQuery uses to find the string within the cloned row (to replace) is basically regex. Thus I just needed to escape the square brackets and period. Once I did this I was able use JQuery to create form as Phil Haack's blog suggested.
我最终意识到(腮红)JQuery用于在克隆行中找到字符串(替换)的机制基本上是正则表达式。因此,我只需要摆脱方括号和句号。一旦我这样做,我就可以使用JQuery创建表单,就像Phil Haack的博客所建议的那样。
Onto my next issue...!
在我的下一期......上!
#5
0
I would number them Amount1, Amount2, Amount3 etc.
我会给他们编号Amount1,Amount2,Amount3等。
#6
0
You can change the id and name attribute of the input to something like this "Amount[1]","Amount[2]","Amount[3]" (yes, the id and name attribute can contain the special chars "[" or "]"). Then in the controller, write a http request parameter parser to get back the Amounts as collections.
您可以将输入的id和name属性更改为“Amount [1]”,“Amount [2]”,“Amount [3]”(是的,id和name属性可以包含特殊字符“[ “ 要么 ”]”)。然后在控制器中,编写一个http请求参数解析器以将Amounts作为集合返回。
#1
12
Old question, but still... You can get the posted values as an array by calling Request.Form.GetValues
, or Request.QueryString.GetValues
. For example:
旧问题,但仍然......您可以通过调用Request.Form.GetValues或Request.QueryString.GetValues将发布的值作为数组获取。例如:
string[] amounts = Request.Form.GetValues("Amount");
And the amounts
array will contain the correct values, so you can post values containing comas, dots, whatever, and don't worry about splitting/parsing it.
amount数组将包含正确的值,因此您可以发布包含comas,dots等的值,并且不用担心拆分/解析它。
Of course if you are running MVC, use the modelbinder to do it. But you can use this if you are using webforms, a generic handler, etc...
当然,如果您正在运行MVC,请使用modelbinder来执行此操作。但是如果你使用webforms,通用处理程序等,你可以使用它...
#2
6
Check out Model Binding To A List. Your Action method should be:
查看模型绑定到列表。你的Action方法应该是:
public ActionResult MyAction(string[] Amount, int[] Item){
// ...
}
However this will make you need to "link" the items. Alternatively create a "Item" class:
但是,这将使您需要“链接”项目。或者创建一个“Item”类:
public class Item {
public string Amount { get; set; }
public int Item { get; set; }
}
And
和
public ActionResult MyAction(IList<Item> items){
// ...
}
And your markup should be:
你的标记应该是:
<input type="hidden" name="items.Index" value="0" />
<input type="text" name="items[0].Amount" id="items[0].Amount">
<select name="items[0].Item">
<option value="1">Item 1"</option>
<option value="2">Item 2"</option>
<option value="3">Item 3"</option>
</select>
<input type="hidden" name="items.Index" value="1" />
<input type="text" name="items[1].Amount" id="items[1].Amount">
<select name="items[1].Item">
<option value="1">Item 1"</option>
<option value="2">Item 2"</option>
<option value="3">Item 3"</option>
</select>
Etc...
等等...
#3
4
I believe if you have multiple fields named Amount the values will be comma delimited.
我相信如果您有多个名为Amount的字段,则值将以逗号分隔。
To access each one just try:
要访问每个只是尝试:
string[] amounts = Request.Form["Amount"].Split(new char[] { ',' });
Keep in mind though, the inputs are not cleaned on submission so if someone enters a comma into the text box it's going to cause issues.
但请记住,输入不会在提交时清除,因此如果有人在文本框中输入逗号,则会导致问题。
Hence I'd recommend numbering them.
因此我建议给它们编号。
#4
1
I ended up realising that (blush) the mechanism which JQuery uses to find the string within the cloned row (to replace) is basically regex. Thus I just needed to escape the square brackets and period. Once I did this I was able use JQuery to create form as Phil Haack's blog suggested.
我最终意识到(腮红)JQuery用于在克隆行中找到字符串(替换)的机制基本上是正则表达式。因此,我只需要摆脱方括号和句号。一旦我这样做,我就可以使用JQuery创建表单,就像Phil Haack的博客所建议的那样。
Onto my next issue...!
在我的下一期......上!
#5
0
I would number them Amount1, Amount2, Amount3 etc.
我会给他们编号Amount1,Amount2,Amount3等。
#6
0
You can change the id and name attribute of the input to something like this "Amount[1]","Amount[2]","Amount[3]" (yes, the id and name attribute can contain the special chars "[" or "]"). Then in the controller, write a http request parameter parser to get back the Amounts as collections.
您可以将输入的id和name属性更改为“Amount [1]”,“Amount [2]”,“Amount [3]”(是的,id和name属性可以包含特殊字符“[ “ 要么 ”]”)。然后在控制器中,编写一个http请求参数解析器以将Amounts作为集合返回。