Hi guys, My problem is that i have a dynamic loop in which i generate 5 textboxes and now i need get the data of each index because i want to send by in a array to the controller. Can anybody tell me how i get the values from the textbox arrays. All textboxes have the same name and id but they have an index.
Firstly, you should NEVER have multiple elements with the same ID. If you want, they can all have the same CLASS though.
UPDATED to reflect clarified requirements.
However, to solve your issue, you can try this:
$("#yourformID").bind("submit", function() {
var groups = new Array();
$("textarea").each(function(i, el) {
name = $(el).attr("name");
index = $(el).attr("index"); // Is 'index' an attr?
if (groups[name] === undefined) {
groups[name] = new Array();
}
groups[name][index] = $(el).val();
});
// You can now insert the array as the value of the submit button...
$(this).find("input[type=submit]").val(groups);
return true;
});
I'm not sure what you meant by 'index', but if that is an attribute on the textarea, then this should get you close.
Of course, on the server-side, you'll need to de-compose the array of arrays, but at this point each item should be easily referenced by name and index.
Good luck!
You can iterate over HttpContext.Request.Form.AllKeys
in your controller like:
foreach(var key in HttpContext.Request.Form.AllKeys) {
if(key.StartsWith("textboxname")) { // key will look like textboxname[1]
// value is in
var val = HttpContext.Request.Form[key];
}
}
just follow the haacked explanation on binding to collections
and create your inputs the right way now need to further parse it at the client side
With index i mean e.g. Textbox[0], Textbox[1] Thx for your help but i found a controller solution