【问题标题】:MVC submit button is always submitting the first record details on POSTMVC 提交按钮始终在 POST 上提交第一条记录详细信息
【发布时间】:2020-07-30 02:28:53
【问题描述】:

我在 MVC 页面中有一个 foreach 循环。如下所示。但是,当我使用提交按钮提交表单时,它总是提交第一条记录。无法理解为什么会这样。感谢您的帮助。

@model IEnumerable<DeployModel>
 
 
@using (Html.BeginForm("Index", "Dep", FormMethod.Post))
{
     
        <div class="col-md-11">
            <!-- Modal content-->
            <div class="modal-content">
               
                <div class="modal-body" id="modal-body" style="overflow-y: auto">
              
                    
                    @if (Model != null && Model.Any())
                    {
                         
                        <div>

                            <table class="table table-condensed table-hover table-bordered">
                                <thead>
                                <th>
                                    S1 Name
                                </th>
                                <th>
                                    S2 Name
                                </th>
                                <th>
                                    Old Version
                                </th>
                                <th>
                                    New Version
                                </th>
                                
                                <th>
                                    Status
                                </th>
                                
                                </thead>
                                <tbody>
                                    @foreach (var item in Model)
                                    {
                                        <tr>
                                            <td>
                                                @Html.DisplayFor(o => item.S1)
                                            </td>

                                            <td>
                                                @Html.DisplayFor(o => item.S2)
                                            </td>

                                            <td>
                                                @Html.DisplayFor(o => item.OldVersion)
                                            </td>

                                            <td>
                                                @Html.DisplayFor(o => item.NewVersion)
                                            </td>
                                            <td>
                                                @Html.DisplayFor(o => item.Status)
                                            </td>

                                           
                                            <td>
                                               
                                          <button name="SubmitButton" class="btn action-btn" type="submit" > Submit                                                </button>
                                            </td>
                                             
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </div>
                    }
                </div>
               
            </div>

        </div>
    </div>

}

我的控制器动作看起来像,但无论按钮点击如何,它总是接收第一条记录。

[HttpPost]
public ActionResult Index(DeployModel deployModel)
{
   //TODO code
}

知道如何解决这个问题吗?

【问题讨论】:

  • 你必须给他们一个不同的name才能工作
  • 我必须在哪里提供不同的名称?能否请您详细说明一下?

标签: c# asp.net-mvc model-view-controller controller


【解决方案1】:

这里有几个问题。第一个是Html.DisplayFor 只会为所选属性的值写入显示字符串,而不会告诉浏览器这是要提交的表单数据。另一个是您所有的提交按钮都是针对同一个表单的。有几种方法可以解决这些问题。我建议为DeployModel 的每个实例创建一个单独的表单,并为您在提交表单时期望收到的每个属性创建隐藏输入:

// inside your foreach loop
@using (Html.BeginForm("Index", "Dep", FormMethod.Post))
{
    <input type="hidden" name="S1" value="@item.S1" />
    <input type="hidden" name="S2" value="@item.S2" />
    ...
    <input type="submit" value="Submit" />
}

另外请注意,如果您使用这种方法,您应该删除外部表单 - 您不想要嵌套表单!

【讨论】:

    【解决方案2】:

    您必须为每个输入提供不同的名称。

    例子

    @Html.DisplayFor(o => item.S1, new { name = "input1" })
    

    【讨论】:

      猜你喜欢
      • 2016-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-01
      • 2013-03-05
      • 1970-01-01
      相关资源
      最近更新 更多