Converting formCollection array to objects in the controller

Posted by bergin on Stack Overflow See other posts from Stack Overflow or by bergin
Published on 2010-05-30T02:21:45Z Indexed on 2010/05/30 2:32 UTC
Read the original article Hit count: 772

Filed under:
|
|
|

in my view I have several [n].propertyName array fields I want to turn the formCollection fields into objects myobject[n].propertyName when it goes to the controller.

so for example, the context:

View:

foreach (var item in Model.SSSubjobs.AsEnumerable())

<%: Html.Hidden("["+c+"].sssj_id", item.sssj_id )   %>
<%: Html.Hidden("["+c+"].order_id", item.order_id ) %>
<%: Html.TextBox("["+c+"].farm", item.farm %>
<%: Html.TextBox("["+c+"].field", item.field %>

c++;

Controller:

I want to take the above [0].sssj_id and turn into sssj[0].sssj_id or a list of sssj objects

My first idea was to look in the form collection for things starting with "[" but I have a feeling this isnt right...

this is as far as I got:

 public IList<SoilSamplingSubJob> extractSSSJ(FormCollection c)
        {
            IList<SoilSamplingSubJob> sssj_list=null;
            SoilSamplingSubJob sssj;


                var n=0;
                foreach (var key in c.AllKeys)   // iterate through the formcollection 
                {
                    var value = c[key];

                    if(key.StartsWith("[")) // ie turn [0].gps_pk_chx into sssj.gps_pk_chx
                       ???
                }



            return sssj_list;
        }

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about mvc