首次加载页面时,JavaScript代码将无法正常工作,但是当我进行一些分页时,它将运行良好

时间:2014-04-07 17:07:16

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

我的sp.net mvc web应用程序中有以下索引视图: -

@model IPagedList<TMS.Models.VirtualMachine>

@{
    ViewBag.Title = "Virtual Machine List";
}

<h2>Virtual Machine List</h2>
<i class=" icon-plus"></i>@Html.ActionLink("Create New", "Create") | @Html.ActionLink("Assets","Asset")

//code goes here
@Html.Partial("**_PagedVMTable**",Model)

和以下_PagedVMTable PartialView: -

@model IPagedList<TMS.Models.TMSVirtualMachine>
<div id ="VMTable">

                        <div class="pagedList" data-tms-target="#VMTable">
                         @Html.PagedListPager(Model , page => Url.Action("Index","VirtualMachine", new { searchTerm = ViewBag.searchTerm , page,sort = ViewBag.CurrentSortOrder }),
                         PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(PagedListRenderOptions.ClassicPlusFirstAndLast, new AjaxOptions { UpdateTargetId = "VMTable" , LoadingElementId="progress2" }))

                        </div>  
                             <img src="~/Content/Ajax-loader-bar.gif" class="loadingimage" id="progress2" />

                        @Html.Partial("~/Views/VirtualMAchine/_VMTable.cshtml",Model)
                    <img src="~/Content/Ajax-loader-bar.gif" class="loadingimage" id="progress3" />
                             <div class="pagedList" data-tms-target="#VMTable">
                         @Html.PagedListPager(Model , page => Url.Action("Index","VirtualMachine", new { searchTerm = ViewBag.searchTerm , page,sort = ViewBag.CurrentSortOrder }),
                         PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(PagedListRenderOptions.ClassicPlusFirstAndLast, new AjaxOptions { UpdateTargetId = "VMTable" , LoadingElementId="progress3"}))

                        </div>  
</div></div></div>
    </div>
<script type="text/javascript">
        $(document).ready(function () {

            $("#ServerSort").click(function () {

                $.ajax({
                    type: "Get",
                    url: "@Url.Action("Index","VirtualMachine")",

                    data: { searchTerm:  "@ViewBag.searchTerm.ToString()" , page:"1" , sort: "@ViewBag.ServerSortPam" },
                    //contentType: "application/json; charset=utf-8",
                    //dataType: "json",
                    cache: false,
                    success: successFunc,
                    error: errorFunc
                });

                function successFunc(data, status) {     
                    $("#VMTable").html(data);
                }

                function errorFunc() {
                    alert('error');
                }
            })
            $("#RackSort").click(function () {

                $.ajax({
                    type: "Get",
                    url: "@Url.Action("Index","VirtualMachine")",

                    data: { searchTerm: "@ViewBag.searchTerm.ToString()", page: "1", sort: "@ViewBag.RackSortPam" },
                    //contentType: "application/json; charset=utf-8",
                    //dataType: "json",
                    cache: false,
                    success: successFunc,
                    error: errorFunc
                });

        function successFunc(data, status) {
            $("#VMTable").html(data);
        }

        function errorFunc() {
            alert('error');
        }
    })
        });
    </script>

现在我面临以下问题: -

  1. 当页面首次加载时,当我点击“ServerSort”或“RackSort”时,JavaScript不会触发。但是当我点击分页链接时,单击“ServerSort”或“RackSort”链接时将触发javaScript。所以不确定为什么会发生这种情况?
  2. 由于

0 个答案:

没有答案