脚本用于多个异步Html.BeginForm



我准备了一个包含产品及其详细信息的页面。当您按下按钮时,将显示详细信息。使用foreach循环,我生成了包含产品和指向详细信息的链接的表。不幸的是,只有顶部表单异步工作。其他打开partialView。我应该怎么做,使脚本工作的所有形式?

<table>
<thead>
<tr>
    <th>
      IdPromo
    </th>
    <th>
      Name
    </th>
  </tr>
</thead>
<tbody>
@foreach (var item in Model.crosspromocje)
{ 
  <tr>
    <td>
      @using (Html.BeginForm("Details", "promo", FormMethod.Post, new { id = "my-form"} ))
       { 
       @Html.Hidden("IdPromo ", item.IdPromo)
        <input type="submit" value="@item.IdPromo" />
       }
    </td>
  </tr>
}
</tbody>
</table>    <div id="result"></div>
@section scripts{
    <script type="text/javascript">
        $(function () { 
            $("#my-form").on("submit", function (e) {
                e.preventDefault();
                $.ajax(
                    {
                        url: this.action,
                        type: this.method,
                        data: $(this).serialize(),
                        success: function (data) {
                            $("#result").html(data);
                        }
                    });
            });
        });
    </script>
}

"

我找到了答案。有必要将id更改为类。来自:

@using (Html.BeginForm("Details", "xp", FormMethod.Post, new { id = "my-form}))
$("#my-form").on("submit", function (e)

:

@using (Html.BeginForm("Details", "xp", FormMethod.Post, new { @class = "my-form" }))
$(".my-form").on("submit", function (e)

最新更新