views:

24

answers:

4

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.

A: 

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!

mkoistinen
He talks about his controller, so I guess this isn't a clientside issue.
Jan Jongboom
He also tagged this jQuery. So I think it is?
mkoistinen
Well, I'm not really sure now...
mkoistinen
My Problem is that i have not only one textbox in my loop. I have 5 text textboxes for each time the loop get passed and i need one array with these 5 values and then i need an array of these Arrays to pass them to my controller.
Konrad
A: 

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];
    }
}
Jan Jongboom
A: 

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

Avi Pinto
A: 

With index i mean e.g. Textbox[0], Textbox[1] Thx for your help but i found a controller solution

Konrad
Konrad, could you explain what your solution was? It seems this is a common problem that isn't answered well.
Tim Joseph