简体   繁体   English

使用MVC从动态表单获取发布数据

[英]Getting Posted Data from a Dynamic Form with MVC

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. 我有一个包含表单的页面,其中部分表单是根据SKU在订单上的动态生成的。

<% 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? 我正在苦苦挣扎的部分是return_<%=i.SKUN%>是一系列动态生成的文本框,它们随每个顺序而变化,尽管它们保留了return_<%=i.SKUN%>的命名约定,如何在我的控制器中获取处理表单帖子的值?

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. 编辑:同样重要的是要注意,这些字段都不是必填字段,文本框的数量因订单而异。

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. 其中index是从0到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. 并让默认的模型绑定器完成工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM