javascript - MVC3 - 只有第一行链接适用于 Jquery 模态对话框

标签 javascript jquery asp.net-mvc-3 razor

使用 MVC3、Razor、Jquery、Javascript。下面的代码循环显示一个包含字段和链接的表结构。每行上的链接都会触发一个 Jquery 模式对话框,该对话框将部分 View 页面显示为弹出窗口。但是弹出对话框仅适用于第一行......第二行及以下的链接将页面作为完整的网页打开,而不是弹出模式对话框。如何解决这个问题..感谢您的帮助。

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Title)
    </td>
    <td>
         @Html.DisplayFor(modelItem => item.Category)
    </td>

    <td>
        @Html.ActionLink("Edit", "Edit", new { id = item.ID }, new { id = "modalEdit" })   |             

    </td>
</tr>

这是 Jquery 模式对话框代码。

<script type="text/javascript">
$(document).ready(function () {
    //initialize the dialog
    $("#resultEdit").dialog({ modal: true, width: 300, resizable: true, position: 'center', title: 'Edit Information', autoOpen: false, 
    open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); }

    });
});

$(function () {
    $('#modalEdit').click(function () {
        //load the content from this.href, then turn it into a dialog.
        $('#resultEdit').load(this.href).dialog('open');
        return false;
    });
});

最佳答案

这可能是因为您所有的编辑链接都具有相同的 id!

这将使 jquery 的行为变得非常不可预测!

改为给您的编辑链接一个共享类,如下所示:

@Html.ActionLink("Edit", "Edit", new { id = item.ID }, new { @class = "modalEdit" }) 

并将您的选择器更改为:

$('.modalEdit').click(function () {

关于javascript - MVC3 - 只有第一行链接适用于 Jquery 模态对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6333851/

相关文章:

javascript - 语法错误,无法识别的表达式

asp.net-mvc-3 - 查看方法指定模型

c# - 使用存储过程进行多重映射的 Dapper

jquery - 通过交替点击对 JSON Z-A 进行排序

javascript - 服务人员 : How to cache the first (dynamic) page

javascript - 对象和数组的复杂 JSON 嵌套

javascript - 替换 observableArray 中的项目

javascript - CKEditor 多个ID的一个实例

c# - 集合被修改;枚举操作可能无法执行

javascript - 动态加载后的jquery设计