0

I have a page that contains a form, where part of it is dynamically generated based off of what SKU's are on an order.

<% for each i in ViewData.Model %>
                    <script type="text/javascript">
                        $(function () {
                            $('#return_<%=i.SKUN%>').change(function () {
                                if ($('#return_<%=i.SKUN%>').val() > $('#qty_<%=i.SKUN%>').val()) {
                                    $('#Discrepancy').val("Yes");
                                } else {
                                    $('#Discrepancy').val("");
                                }
                            });
                        });
                    </script>
                    <tr>
                        <td style="text-align: left"><%= i.SKUN%></td>
                        <td style="text-align: left; width: 360px"><%= i.DESCR%></td>
                        <td style="text-align: left">&pound;<%= i.PRIC%></td>
                        <td style="text-align: left"><%= i.QUAN%></td>
                        <td style="text-align: left">&pound;<%= i.EXTP%></td>
                        <td style="text-align: left"><input type="hidden" name="qty_<%=i.SKUN%>" id="qty_<%=i.SKUN%>" value="<%= i.QUAN%>"/><input type="text" name="return_<%=i.SKUN%>" id="return_<%=i.SKUN%>" style="width:50px;" class="required"/>
                            <%  If i.FLAG3 = "T" Then
                                   %> <img src="../../Content/images/icons/error.png" alt="This SKU is subject to a <%=Html.ViewData("RestockFee") %>% restocking fee" title="This SKU is subject to a <%=Html.ViewData("RestockFee") %>% restocking fee"/><%
                                End If%>
                        </td>
                    </tr>
                    <% Next%>

It's by no means perfect, but it gets the job done at the moment.

The part I'm struggling with is as return_<%=i.SKUN%> is a series of dynamically generated text boxes that change for each order, though they remain with the naming convention of return_<%=i.SKUN%>, how do I get the values for them in my controller that handles the form post?

EDIT: It's also important to note that none of these fields are required fields and that the number of text boxes varies per order.

LiamGu
  • 5,317
  • 12
  • 49
  • 68

1 Answers1

2

Can't you change the naming convention to:

<input 
    type="text"
    name="skuns[<%= index %>]"
    id="return_<%= i.SKUN %>"
    style="width:50px;"
    class="required"
    value="<%= i.SKUN %>"
/>

where index would be an incrementing variable from 0 to n. This way your controller action could look like this:

Public Function Result(skuns As String()) As ActionResult

And leave the default model binder do the job.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • If the default binder has difficulties, you could enumerate over Request.Form.Where(q=>q.Name.StartsWith("skuns[")); – Amethi Nov 16 '10 at 09:39
  • As long as you respect the naming convention the default model binder never has difficulties and using strongly typed action arguments is better. – Darin Dimitrov Nov 16 '10 at 09:40