0

我已经在我的 asp.net mvc 中安装了 PagedList.mvc 分页库。我添加了以下脚本:-

var getPage = function () {
    var $a = $(this);
    var options = {
        url: $a.attr("href"),
        type: "get"
    };
    $.ajax(options).done(function (data) {
        var target = $a.parent("div.pagedList").attr("data-tms-target");
        $(target).replaceWith(data);
    });
    return false;
};



$(".main-content").on("click", ".pagedList a", getPage);

主视图如下所示:-

@model IPagedList<TMS.Models.AuditInfo>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<div id="AuditTable">
@Html.Partial("_AuditTable", Model)
    </div>

_AuditTable 部分视图是:-

  @model IPagedList<TMS.Models.AuditInfo>
    <div class="row-fluid sortable" >       
    <div class="box span12">
    <div class="box-header well" data-original-title id="auditlist">
    <h2><i class="icon-home"></i> Audit Info </h2>
    <div class="box-icon">
    <a href="#" class="btn btn-setting btn-round"><i class="icon-cog"></i></a>
    <a href="#" class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>
    <a href="#" class="btn btn-close btn-round"><i class="icon-remove"></i></a>
    </div><div class="box-content">
     <div class="pagedList" data-tms-target="#AuditTable">
      @Html.PagedListPager(Model , page => Url.Action("Index",new { page }),
                            PagedListRenderOptions.ClassicPlusFirstAndLast)

                            </div>
        <table class="table table-striped table-bordered bootstrap-datatable datatable">
                              <thead>
                                  <tr>
           <th>
                User Name
            </th>
            <th>
                Audit Action
            </th>
            <th>
                Technology Type
            </th>
            <th>
                Technology
            </th>
            <th>
                Date Time Start
            </th>
            <th>
                Date Time End
            </th>
        </tr>
     </thead>
        <tbody>
              @foreach (var item in Model) {
        <tr>

            <td>
                @item.UserName
            </td>
            <td>
                @(item.AuditAction == null ? "None" : item.AuditAction.Name)
            </td>
            <td>
                @(item.TechnologyType == null ? "None" : item.TechnologyType.Name)
            </td>
            <td>
                @Html.DisplayTextFor(_ => item.Technology.TechnologyID).ToString()
            </td>
            <td>
                @Html.DisplayFor(model=>item.DateTimeStart)
            </td>
             <td>
                @Html.DisplayFor(model=>item.DateTimeStart)
            </td>
        </tr>
    }

    </tbody>
    </table>
    </div></div></div>

但问题是,当我点击页面链接时,什么都不会发生,虽然链接的 href 将指向正确的链接,但是如果我点击链接,什么都不会加载,而如果我右键点击链接并点击打开它将打开页面。那么我该如何克服这个问题呢?

最后的行动方法是: -

public ActionResult Index(int page = 1)
        {
             var audit = auditinfoRepository.AllIncluding(auditinfo => auditinfo.Technology,auditinfo2=>auditinfo2.TechnologyType,auditinfo3=>auditinfo3.AuditAction).OrderByDescending(a=>a.DateTimeStart)
                .ToPagedList(page,30);
             if (Request.IsAjaxRequest())
             {
                 ViewBag.FromSearch = true;
                 return PartialView("_AuditTable", audit);
             }
             return View(audit);
        }
4

1 回答 1

0

默认情况下,ASP.NET MVC 3 使用带有所有Ajax.*帮助程序的不显眼的 jquery。因此,首先摆脱所有 MicrosoftAjax 脚本(这个无用的 c**p),然后改为:

<script src="@Url.Content("~/Scripts/jquery-1.5.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

然后只需在 web.config 中激活不显眼的 AJAX(如果尚未完成):

<appSettings>
    <add key="ClientValidationEnabled" value="true"/> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/> 
</appSettings>

现在 jquery 将不显眼地 AJAXify 包含那些 HTML 5data-*属性的所有链接。

于 2013-07-15T04:16:58.863 回答