【问题标题】:How to submit data as a list from this table to action in asp.net mvc? [closed]如何将此表中的数据作为列表提交到asp.net mvc中的操作? [关闭]
【发布时间】:2018-07-10 03:03:21
【问题描述】:
 <table id="example1" class="table table-bordered table-striped">
                        <thead>
                            <tr>
                                @*<th style="width:0px"></th>*@
                                <th>Name</th>
                                <th>Quantity</th>
                                <th>Unit Price</th>
                                <th>Total Price</th>
                                <th>Company Name</th>
                                <th style="display:none"></th>

                                <th></th>
                            </tr>
                        </thead>
                        <tbody>

                           @foreach (var item in list)
                        {

                                <tr>

                                    <td name="pname">@item.Material_Name</td>
                                    <td name="pprice">@item.Quantity</td>
                                    <td name="pdetail">@item.Unit_Price</td>
                                    <td name="pdetail">@item.Total_Price</td>
                                    <td name="pdetail">@item.Name</td>
                                    <td name="pid" style="display:none; white-space:nowrap">item.Id</td>

                                </tr>
                           }

                        </tbody>
                    </table>
                    <button class="btn btn-primary btn-lg btn-block" name="save" type="button">Print</button>

我想在操作时从数据表中获取产品列表。获得列表后,我想打印该列表。现在我只想从数据表中获取列表到操作。

【问题讨论】:

    标签: javascript jquery asp.net asp.net-mvc asp.net-mvc-4


    【解决方案1】:

    你需要使用 BeginForm

    然后单击提交按钮,您可以在 Post Action 方法中看到您的数据。

      @model IList<PaymentSchedule>
    
      @using (Html.BeginForm("SubmitDataToUpload", "Payment", FormMethod.Post, new {enctype = "multipart/form-data"}))
      {
    
                  <table id="example1" class="table table-bordered table-striped">
                        <thead>
                            <tr>
                                @*<th style="width:0px"></th>*@
                                <th>Name</th>
                                <th>Quantity</th>
                                <th>Unit Price</th>
                                <th>Total Price</th>
                                <th>Company Name</th>
                                <th style="display:none"></th>
    
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>
    
                           /*@foreach (var item in list)*/
            /* foreach loop does not generate the correct name attributes */
                       for(int i = 0; i < Model.Count; i++)
                        {
    
                                <tr>
                               <td>@Html.TextBoxFor(m =>Model[i].Material_Name)</td>
                               <td>@Html.TextBoxFor(m =>Model[i].Quantity)</td>
                               <td>@Html.TextBoxFor(m =>Model[i].Unit_Price)</td>
                               <td>@Html.TextBoxFor(m =>Model[i].Price)</td>
                               <td>@Html.TextBoxFor(m =>Model[i].Company)</td>
                               <td>@Html.TextBoxFor(m =>Model[i].Id)</td>         
    
                                </tr>
                           }
    
                        </tbody>
                    </table>
    
    
    
                  /*Update : Added Submit Button*/
    
                  <input class="btn btn-success" type="submit" value="Submit"/>
                   
    
           }
    
    
         public ActionResult SubmitDataToUpload(List<PaymentSchedule> paymentSchedules)
         {
            //Add to paymentSchedule
            return View("ViewName",paymentSchedules);
         }
    

    foreach 循环不会生成正确的名称属性以获取更多信息: Review This Link

    【讨论】:

    • 您没有表单控件,因此没有提交任何内容(type="button" 无论如何也不会提交表单)
    • 更新ans,谢谢指正
    • 您的表单中仍然没有表单控件,因此根本没有向服务器提交任何内容
    • 不好意思,谢谢指出,今天终于有新东西要学了!
    • 这仍然不起作用 - 您不能使用 foreach 循环 - 请参阅 Post an HTML Table to ADO.NET DataTable 了解原因
    猜你喜欢
    • 2021-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-20
    相关资源
    最近更新 更多