【问题标题】:jQuery Javascript Modal Popup not working in MVCjQuery Javascript Modal Popup 在 MVC 中不起作用
【发布时间】:2020-03-30 20:30:08
【问题描述】:

当我单击“评论”链接时,会打开一个模式弹出窗口并显示内容,因此它会执行应有的操作。

转到我的问题: 在第一种情况下,它发生了我想要的,但其他一些代码没有实现。在这种情况下,我将“@section”放在 Index.cshtml 中,并且模式弹出窗口以一些样式呈现在页面中间(这是我想要的),但 @section 上方的数据表和 yadcf 代码没有似乎被执行(例如,数据表过滤器不存在)。我在名为“Wanted Modal Popup”的 jpeg 中展示了这个场景。

另一方面,在第二种情况下,如果我将@section 放在“ViewAll”视图中,数据表可以正常工作,模态弹出窗口也会出现,但它确实像第一种情况那样出现。我在名为“Unwanted Modal Popup”的 jpeg 中展示了第二种情况。

我想将代码留在 ViewAll 视图中(第二种情况),因为这是与模式弹出窗口相关的代码。另外,当我这样做时,会出现附加错误。我查看了错误,似乎可能有一些不兼容?如果是这样,为什么它会在索引页面上工作呢?

你能帮忙吗?我花了很多时间试图解决这个问题,但没有成功。

布局查看代码:

<!DOCTYPE html>
        <html>
        <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>@ViewBag.Title - My ASP.NET Application</title>
        @Styles.Render("~/Content/css")
        @Scripts.Render("~/bundles/modernizr")
        <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" 
        rel="stylesheet" />
</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                @Html.ActionLink("Helpdesk Support", "Index", "Tickets", null, new { @class = "navbar-brand" })
            </div>

        </div>
    </div>

    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - Nicola's Application</p>
        </footer>
    </div>


    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")

    <script src="~/Scripts/Scripts.js"></script>

    @RenderSection("scripts", required: false)
    @RenderSection("scriptsKnowledge", required: false)
    @RenderSection("modalComments", required: false)

</body>
</html>

INDEX.CSHTML 查看代码:

@{
    ViewBag.Title = "Index";
}

<ul class="nav nav-tabs">
    <li class="active"><a data-toggle="tab" href="#firstTab">View All</a></li>
    <li><a data-toggle="tab" href="#secondTab">Add New</a></li>
    <li><a data-toggle="tab" href="#thirdTab">Knowledge</a></li>
    <li><a data-toggle="tab" href="#fourthTab">IT Tasks</a></li>
</ul>

<div class="tab-content">
    <div id="firstTab" class="tab-pane fade in active">@Html.Action("ViewAll")</div>
    <div id="secondTab" class="tab-pane fade in">@Html.Action("AddOrEdit")</div>
    <div id="thirdTab" class="tab-pane fade in">@Html.Action("ViewAllKnowledge")</div>
    <div id="fourthTab" class="tab-pane fade in">@Html.Action("ViewAllTasks")</div>
</div>



@*jQuery Datatable CSS*@
<link href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">                @*for the visual style of the datepicker of the dtLastUpdated column*@
<link rel="stylesheet" href="https://cdn.datatables.net/responsive/2.2.3/css/responsive.dataTables.min.css">    @*For column reorder functionality*@
<link rel="stylesheet" href="https://cdn.datatables.net/colreorder/1.5.2/css/colReorder.dataTables.min.css">    @*For column reorder functionality*@


@section scripts
{
    @Scripts.Render("~/bundles/jqueryval")

    @*jQuery Datatable JS*@
    <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.js"></script>
   <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>                                  
    <script src="https://cdn.datatables.net/responsive/2.2.3/js/dataTables.responsive.min.js"></script>     
    <script src="https://cdn.datatables.net/colreorder/1.5.2/js/dataTables.colReorder.min.js"></script>     


    <script>
        function activatejQueryTable() {

            var table = $('#ticketTable').DataTable({
                rowReorder: { selector: 'tr' }, 
                colReorder: true,               
                "stateSave": true,              
                "stateDuration": 0,             
                "autoWidth": false,             
                "columnDefs": [
                    { "width": "40px", "targets": 0, "visible": true },     
                    { "width": "150px", "targets": 1, "visible": true },    //dtLastUpdated
                    { "width": "250px", "targets": 2, "visible": true },    //vcSubject
                    { "width": "150px", "targets": 3, "visible": true },    //vcFrom
                    { "width": "53px", "targets": 4, "visible": true },     //vcPriority
                    { "width": "90px", "targets": 5, "visible": true },     //vcAssignedTo
                    { "width": "53px", "targets": 6, "visible": true },     //vcStatus
                    { "width": "90px", "targets": 7, "visible": true },     //vcRequestType
                    { "width": "90px", "targets": 8, "visible": true },     //vcLocation
                    { "width": "90px", "targets": 9, "visible": true },     //vcCategory
                    { "width": "90px", "targets": 10, "visible": true },    //dtAnticipatedCompletion
                    { "width": "50px", "targets": 11, "visible": true }     //edit and delete buttons
                ],

                //Create the dropdowns
                responsive: true,
               "bAutoWidth": false,            
                initComplete: function () {

                    this.api().columns([4, 5, 6, 7, 8, 9]).every(function () {      //this is to put a dropdown just for some columns
                        var column = this;

                        var select = $('<select class="myDropdown"><option value=""></option></select>')

                            .appendTo($("#filters").find("th").eq(column.index()))
                            .on('change', function () {
                                var val = $.fn.dataTable.util.escapeRegex($(this).val());
                                column.search(val ? '^' + val + '$' : '', true, false).draw();
                            })
                            .on('click', function (e) {
                                e.stopPropagation();                                //this event handler catches the click and then prevents propagation. Without this, the sort of the column happens every time you click on the dropdown
                            });

                        column.data().unique().sort().each(function (d, j) {
                            $(select).append('<option value="' + d + '">' + d + '</option>')
                        });
                    });
                }
                //End of create dropdowns
            });



            //Datepicker for dtLastUpdated column, which has id="datepicker" and class="datepicker"
            $(".datepicker").datepicker({
                maxDate: 0,
                changeMonth: true,
                changeYear: true,
                dateFormat: 'mm/dd/yy',
                onClose: function (selectedDate) {
                    table.draw();
                }
            }).on("input change", function () {

                filterGlobal();
            });
        }
        //});


        //Also part of Datepicker for dtLastUpdated column
        function filterGlobal() {
            $('#ticketTable').DataTable().search(
                $(".datepicker").val()
            ).draw();
        }



        $(function () {
            activatejQueryTable();
        });     

    </script>
}



@section scriptsKnowledge
        {
    <script src="~/DataTables/datatables.js"></script>
    @*<script src="https://cdnjs.cloudflare.com/ajax/libs/yadcf/0.9.3/jquery.dataTables.yadcf.min.js" integrity="sha256-nX2mPo5tTUWsDUalF2TgsIn8mKkjzrkc/XMHbfpRfF0=" crossorigin="anonymous"></script>*@
    <script src="~/Yadcf/jquery.dataTables.yadcf.js"></script>

    <script>
        function yadcfTable() {
            var oTable;
            oTable = $('#knowledgeTable').DataTable({
                stateSave: true
            });

            yadcf.init(oTable, [
                { column_number: 0 }]);
        }

        $(function () {
            yadcfTable();
        });
    </script>
}



@section modalComments
{
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js" type="text/javascript"></script>
    <link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/blitzer/jquery-ui.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript">
        function showComments() {
            $("#dialog").dialog({
                autoOpen: false,
                modal: true,
                title: "View Details"
            });
            $("#ticketTable .details").click(function () {
                var ticketNum = $(this).closest("tr").find("td").eq(0).html();
                $.ajax({
                    type: "POST",
                    url: "/Tickets/ViewComments",
                    data: '{ticketNum: "' + ticketNum + '" }',
                    contentType: "application/json; charset=utf-8",
                    dataType: "html",
                    success: function (response) {
                        $('#dialog').html(response);
                        $('#dialog').dialog('open');
                    },
                    failure: function (response) {
                        alert(response.responseText);
                    },
                    error: function (response) {
                        alert(response.responseText);
                    }
                });
            });
        };

        $(function () {
            showComments();
        });
    </script>
}

VIEWALL.CSHTML 查看代码:

@model IEnumerable<HelpDeskSupport.Models.Ticket>

@{
    ViewBag.Title = "ViewAll";
    //Layout = null;
    Layout = "~/Views/Shared/_Layout.cshtml";
}


@section modalComments
    {
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js" type="text/javascript"></script>
    <link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/blitzer/jquery-ui.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript">
        function showComments() {
            $("#dialog").dialog({
                autoOpen: false,
                modal: true,
                title: "View Details"
            });
            $("#ticketTable .details").click(function () {
                var ticketNum = $(this).closest("tr").find("td").eq(0).html();
                $.ajax({
                    type: "POST",
                    url: "/Tickets/ViewComments",
                    data: '{ticketNum: "' + ticketNum + '" }',
                    contentType: "application/json; charset=utf-8",
                    dataType: "html",
                    success: function (response) {
                        $('#dialog').html(response);
                        $('#dialog').dialog('open');
                    },
                    failure: function (response) {
                        alert(response.responseText);
                    },
                    error: function (response) {
                        alert(response.responseText);
                    }
                });
            });
        };

        $(function () {
            showComments();
        });
    </script>
}



@*<head>*@
@*<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>*@
@*@Scripts.Render("~/bundles/jquery")*@
@*<script src="~/Scripts/jquery-3.4.1.js"></script>*@
@*<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>*@
@*<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js" type="text/javascript"></script>
    <link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/blitzer/jquery-ui.css" rel="stylesheet" type="text/css" />*@
@*<script type="text/javascript">
        $(function () {
            $("#dialog").dialog({
                autoOpen: false,
                modal: true,
                title: "View Details"
            });
            $("#ticketTable .details").click(function () {
                var ticketNum = $(this).closest("tr").find("td").eq(0).html();
                $.ajax({
                    type: "POST",
                    url: "/Tickets/ViewComments",
                    data: '{ticketNum: "' + ticketNum + '" }',
                    contentType: "application/json; charset=utf-8",
                    dataType: "html",
                    success: function (response) {
                        $('#dialog').html(response);
                        $('#dialog').dialog('open');
                    },
                    failure: function (response) {
                        alert(response.responseText);
                    },
                    error: function (response) {
                        alert(response.responseText);
                    }
                });
            });
        });
    </script>*@
@*</head>*@



@*<input type="submit" value="Clear Filters" id="filterBtn" class="bg-info" onclick="cleanFilters()">*@
<button type="button" onclick="cleanFilters()" value="Clear Filters" id="filterBtn" class="btn btn-primary btn-sm">Clear Filters / Reset</button>


@*jQuery Datatable (and could also be for ThemeRoller*@
<table class="display table table-striped table-bordered" id="ticketTable" style="width:100%">
    <thead>
        <tr id="filters">
            <th>
                @Html.DisplayNameFor(model => model.iNumber)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.dtLastUpdated)
                <input type="text" id="datepicker" class="datepicker">    @*readonly="readonly"*@
            </th>
            <th>
                @Html.DisplayNameFor(model => model.vcSubject)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.vcFrom)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.vcPriority)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.vcAssignedTo)
            </th>
            @*<th>
                    @Html.DisplayNameFor(model => model.dtAssignedDate)
                </th>*@
            <th>
                @Html.DisplayNameFor(model => model.vcStatus)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.vcRequestType)
            </th>
            @*<th>
                    @Html.DisplayNameFor(model => model.vcBody)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.dtDateSubmitted)
                </th>*@
            <th>
                @Html.DisplayNameFor(model => model.vcLocation)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.vcCategory)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.dtAnticipatedCompletion)
            </th>
            <th></th>
        </tr>
    </thead>

    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.iNumber)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.dtLastUpdated)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.vcSubject)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.vcFrom)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.vcPriority)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.vcAssignedTo)
                </td>
                @*<td>
                        @Html.DisplayFor(modelItem => item.dtAssignedDate)
                    </td>*@
                <td>
                    @Html.DisplayFor(modelItem => item.vcStatus)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.vcRequestType)
                </td>
                @*<td>
                        @Html.DisplayFor(modelItem => item.vcBody)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.dtDateSubmitted)
                    </td>*@
                <td>
                    @Html.DisplayFor(modelItem => item.vcLocation)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.vcCategory)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.dtAnticipatedCompletion)
                </td>
                <td>
                   <a class="btn btn-primary btn-sm" onclick="Edit('@Url.Action("EditOneRecord", "Tickets", new { id = item.iNumber })')"><i class="fa fa-pencil fa-lg"></i></a>
                    <a class="btn btn-danger btn-sm" onclick="Delete('@Url.Action("Delete", "Tickets", new { id = item.iNumber })')"><i class="fa fa-trash fa-lg"></i></a>
                    <a class="details" href="javascript:;">Comments</a>
                </td>
            </tr>
        }
    </tbody>
</table>

<div id="dialog" style="display: none">
</div>

【问题讨论】:

    标签: javascript jquery html ajax asp.net-mvc


    【解决方案1】:

    根据您所提供的内容,很难理解您要使用哪个版本的 jquery,但错误消息显示当错误发生时您使用的是 jquery 3.4.1。

    根据https://github.com/vanderlee/colorpicker/issues/132,如果将 Jquery 3.x 与 JqueryUI 1.11 结合使用,则会出现此错误。这里的修复是升级到 JqueryUI 1.12。

    您可能希望删除所有已注释掉的内容并重新发布您的问题;很难注意到代码的哪些部分被注释掉了,这会让其他人很难提出答案。您似乎在使用两个不同版本的 Jquery(1.7.2 与 3.4.1)之间犹豫不决,这对您来说可能是问题的根源。

    【讨论】:

    • 非常感谢您的回答。问题是我有 1.11.4 jquery-ui 而不是更新的。我发现 Jquery 3.4.1 不适用于 jquery 1.11.4,但它适用于 1.12.0。非常感谢您的帮助。
    猜你喜欢
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 2019-01-10
    相关资源
    最近更新 更多