将行信息添加到Ajax.BeginForm确认消息中

时间:2015-08-12 15:31:57

标签: ajax asp.net-mvc

我有一个包含多个列的表。第一列包含位置名称,最后一列包含删除按钮。当我单击任何删除按钮时,它当前显示确认消息“您确定要删除该位置:”我希望确认消息的结束显示位置名称。

以下是我视图中的相关内容:

@using (Ajax.BeginForm("FirstAjax", "Configuration", null, new AjaxOptions()
            {
                Confirm = "Are you sure you want to delete the location:\n",
                HttpMethod = "POST",
                OnFailure = "deleteFailed",
                UpdateTargetId = "CustomLocations"
            },
            new { @id = "deleteLocation" }
            ))
            {
<div id="reportTblDiv">
      <table id="CustomLocations">
         <tbody>
             @foreach (var location in Model.LocationList)
                 {
                      <tr>
                           <td class="location">@location.LocationName</td>
                           <td>
<input type="submit" title=@Model.DeleteButton name="@location.LocationId" value="@Model.DeleteButton" />
</td>
</tr>
      }
      </tbody>
  </table>
</div>
}

如何从以Confirm?

开头的行访问Model.LocationList中的正确元素

1 个答案:

答案 0 :(得分:0)

将第一个的id设置为与第二个名称相同的名称。在javascript中添加提交句柄以更新确认消息:

<td id=@location.LocationId class="location">@location.LocationName</td>
<td>
    <input type="submit" title=@Model.DeleteButton name="@location.LocationId" value="@Model.DeleteButton" onclick="return btnDeleteClicked(this);"/>
</td>

     <script>
        function btnDeleteClicked(btnObject){

        var tLocation=$(btnObject).attr('name');
        var tCurrentMessage=$("#deleteLocation").data('ajax-confirm');

        $("#deleteLocation").data('ajax-confirm',tCurrentMessage + tLocation);

        return true;
       }
        </script>