I am trying to pass multiple values for a single field name to an ajax call:
<form>
<input type="hidden" name="fieldName" value="MyValue1">
<input type="hidden" name="fieldName" value="MyValue2">
<input type="hidden" name="fieldName" value="More data...">
<input type="hidden" name="fieldName" value="More data...">
<input type="hidden" name="fieldName" value="More data...">
<input type="hidden" name="fieldName" value="More data...">
</form>
I cannot simply submit the form because the submission has to be done using ajax for reasons that are not relevant to this situation.
When I serialize this form data, I get the following:
formData: Object
fieldName: Array[6]
0: "MyValue1"
1: "MyValue2"
2: "More data..."
3: "More data..."
4: "More data..."
5: "More data..."
length: 6
So far, this looks good to me. Here is where I submit this to the server:
$.post("/MyHandler.axd", formData, function (data) {
// etc.
});
On the server, code which I do not control does the following:
string[] values = request.Form.GetValues("fieldName");
At this point, values is null. However, if I do this:
string[] values = request.Form.GetValues("fieldName[]");
values has my array of length 6, with the correct data in it, etc.
I understand what is happening here, but I don’t know why or how I can get around it. Is there a way for me to pull out fieldName instead of fieldName[] without submitting the form?
Thanks.
EDIT: After trying @raymondralibi’s suggestions, I am now getting the following:
For the first suggestion (getSerializeArray($(this).closest('form'))), I get this:
formData: Object
: undefined
__proto__: Object
And for the second (getSerializeArray2($(this).closest('form'))), I get this:
formData: Array[1]
0: Object
: undefined
__proto__: Object
length: 1
__proto__: Array[0]
Perhaps I am calling these incorrectly; any thoughts? Thanks again.
Well, I ended up getting this working, but I don’t like how I did it:
Before the code that executes that calls
request.Form.GetValues("fieldName"), I do the following:This copies the ArrayList to the correct key in the hash table, and everything works fine. Still, I would prefer to have this passed correctly from the client side, but for now this works.