Asp.net mvc ASP.NET MVC5 EF6页面列表作为部分返回整个页面,但只是有时返回

Asp.net mvc ASP.NET MVC5 EF6页面列表作为部分返回整个页面,但只是有时返回,asp.net-mvc,Asp.net Mvc,是什么原因导致下面的脚本在需要时随机触发?i、 e.如果我在警报中发表评论(44);当我点击第2页时,它有时会在开始时弹出一次。然后我点击寻呼机中的其他页面,没有提示(44),但我会重新加载整个页面,而不仅仅是部分页面 因此,实际上,这就像我没有问题一样,但前提是我可以在点击寻呼机栏时强制下面的Javascript Click一直运行 $(function () { $('#myPager').on('click', 'a', function () {

是什么原因导致下面的脚本在需要时随机触发?i、 e.如果我在警报中发表评论(44);当我点击第2页时,它有时会在开始时弹出一次。然后我点击寻呼机中的其他页面,没有提示(44),但我会重新加载整个页面,而不仅仅是部分页面

因此,实际上,这就像我没有问题一样,但前提是我可以在点击寻呼机栏时强制下面的Javascript Click一直运行

  $(function () {
        $('#myPager').on('click', 'a', function () {
            //alert(44);
            $.ajax({
                url: this.href,
                type: 'GET',
                cache: false,
                success: function (result) {
                    $('#ResultsList').html(result);
                }
            });
            return false;
        });
    });
上面的Javascript位于“$(document).ready(function(){”和我的主文件index.cshtml的一部分中

同样在index.cshtml中,我有

<div id="ResultsList" style="clear:both;">

    @Html.Partial("IndexSearchResults")

</div>
我有一段代码是从index.aspx中的搜索按钮触发的。这段代码现在只运行一次,在我单击寻呼机后失败,如下所示:

函数performSearch(){

var url='/Premise/IndexSearch';
风险值数据={
searchSPID:$('#searchSPID').val().toString(),
searchPremise:$('#searchPremise').val().toString(),
searchPostcode:$('#searchPostcode').val().toString(),
optVacant:$(“输入[name='optvacantorcupied']:选中”).attr(“id”),
OptOurSpids:$(“输入[name='OptOurSpids']:选中”).attr(“id”),
SearchCompany:$(“#SearchCompany”).val().toString(),
SearchLP:$('#SearchLP').val().toString()
};
$(“#结果列表”).load(url、数据、函数(){
$('#LoadingGif').empty();
});
$('#LoadingGif').empty().html('');
}
由于从dom中删除元素,更新页面时,您的#myPager事件处理程序将丢失。如果要执行此类操作,请始终将dom事件附加到页面中未删除的部分,这样您的处理程序就不会被破坏

尽量靠近原始元素,以阻止大量处理程序减慢页面速度

以下是更新的脚本:

$(function () {
    $('#ResultsList').on('click', '#myPager a', function (evt) {
        var target = $(evt.currentTarget);
        $.ajax({
            url: target.attr("href"),
            type: 'GET',
            cache: false,
            success: function (result) {
                $('#ResultsList').html(result);
            }
        });
        return false;
    });
});

我还没有测试过,但它看起来不错。

谢谢,这对我的寻呼机进行了排序,因为我现在可以单击,并且它的行为正确。但是现在只能单击一次主搜索按钮。就像修复程序受到了某种影响一样。我已经附加了在单击主搜索按钮时启动的Javascript,好的,这是对我的co的一个小改动所以它只返回部分,非常好,谢谢
    public ActionResult Index()
    {

        if (!Request.IsAuthenticated)
            return View("~/Views/Shared/NotAuthorised.cshtml");


        ViewData["TotalRecords"] = _db.FindAllPremises().Count();
        ViewData["ResultCount"] = 0;
        ViewBag.InitialPageCount = 10;

        var obj = _db.FindAllPremiseEmpty();
        return View("Index", obj.ToPagedList(1, 10));


    }

    public ActionResult IndexSearch(int? page, string searchSPID, string searchPremise, string searchPostcode, int optVacant, int OptOurSpids, string searchCompany, string searchLP)
    {
        var searchResults = getSearchResults(searchSPID, searchPremise, searchPostcode, optVacant, OptOurSpids, searchCompany, searchLP);
        ViewData["TotalRecords"] = _db.FindAllPremises().Count();
        ViewBag.ResultCount = searchResults.Count();

        ViewBag.searchSPID = searchSPID;
        ViewBag.searchPremise = searchPremise;
        ViewBag.searchPostcode = searchPostcode;
        ViewBag.optVacant = optVacant;
        ViewBag.OptOurSpids = OptOurSpids;
        ViewBag.searchCompany = searchCompany;
        ViewBag.searchLP = searchLP;

        int pageSize = 5;
        int pageNumber = (page ?? 1);

        if (searchResults == null)
            return View("NotFound");

        if (Request.IsAjaxRequest())
        {
            return PartialView("IndexSearchResults", searchResults.ToPagedList(pageNumber, pageSize));
        }


        return View("Index", searchResults.ToPagedList(pageNumber, pageSize));

    }
    var url = '/Premise/IndexSearch';


    var data = {
        searchSPID: $('#SearchSPID').val().toString(),
        searchPremise: $('#SearchPremise').val().toString(),
        searchPostcode: $('#SearchPostcode').val().toString(),
        optVacant: $("input[name='OptVacantOrOccupied']:checked").attr("id"),
        OptOurSpids: $("input[name='OptOurSpids']:checked").attr("id"),
        SearchCompany: $('#SearchCompany').val().toString(),
        SearchLP: $('#SearchLP').val().toString()
    };

    $("#ResultsList").load(url, data, function () {
        $('#LoadingGif').empty(); 
    });


    $('#LoadingGif').empty().html('<img src="/Content/images/ajax-loader.gif" width=31 height=31 alt="Loading image" />');
}
$(function () {
    $('#ResultsList').on('click', '#myPager a', function (evt) {
        var target = $(evt.currentTarget);
        $.ajax({
            url: target.attr("href"),
            type: 'GET',
            cache: false,
            success: function (result) {
                $('#ResultsList').html(result);
            }
        });
        return false;
    });
});