Windows上的MVC4 Ajax加载记录滚动

Ben*_*Ben 4 ajax asp.net-mvc jquery asp.net-mvc-4

使用MVC4和Ajax。我正在加载5条记录,并在Windows上向下滚动以加载接下来的5条记录..etc

控制器:(有效)

    public JsonResult FetchData(int pageIndex = 0)
    {
      var model = ...
      ViewBag.count = pageIndex*2;

      return Json(model, JsonRequestBehavior.AllowGet);
    }          
Run Code Online (Sandbox Code Playgroud)

视图

javascript:

$(function () {
    $(window).scroll(function () {
        if ($(window).scrollTop() == $(document).height() - $(window).height()) {
            FetchDataFromServer();
        }
    });
});

  function FetchDataFromServer() {
    var Id = $(".postcount").attr("Id");
    $.ajax({
        url: '@Url.Action("FetchData")',
        data: { pageIndex: Id },
        datatype: 'application/json',
        success: function () {
            //?
        },
        error: function () {
            alert("error");
        }
  });


 <div id="result">
   @Html.Partial("_ResultList",Model)
</div>
Run Code Online (Sandbox Code Playgroud)

首次将模型传递到局部视图,并且数据成功加载。向下滚动,将执行Action FeachData,并且可以看到成功检索到数据。

我的问题是当FeachData方法传递模型时,如何将模型传递给Partial View并追加到现有记录?

除模型外的局部视图,并具有一个@foreach(模型中的var项){..},该循环循环并显示数据。

谢谢

小智 5

如果您将模型建模为集合,请使用.Take().Skip()过滤所需的记录,并根据结果返回部分视图。

控制者

public ActionResult FetchData(int skipCount, int takeCount)
{
  var model = db.MyObjects.OrderBy(x => x.SomeProperty).Skip(skipCount).Take(takeCount);
  if (model.Any())
  {
    return PartialView("_ResultList", model);
  }
  else
  {
    return null;
  }
}
Run Code Online (Sandbox Code Playgroud)

脚本

var skipCount = 5; // start at 6th record (assumes first 5 included in initial view)
var takeCount = 5; // return new 5 records
var hasMoreRecords = true;

function FetchDataFromServer() {
  if (!hasMoreRecords) {
    return;
  }
  $.ajax({
    url: '@Url.Action("FetchData")',
    data: { skipCount : skipCount, takeCount: takeCount },
    datatype: 'html',
    success: function (data) {
      if (data === null) {
        hasMoreRecords = false; // signal no more records to display
      } else {
        $("#result").append(data);
        skipCount += takeCount; // update for next iteration
      }
    },
    error: function () {
      alert("error");
    }
  });
}
Run Code Online (Sandbox Code Playgroud)

编辑:使用JSON的替代方法

控制者

public ActionResult FetchData(int skipCount, int takeCount)
{
  var model = db.MyObjects.OrderBy(x => x.SomeProperty).Skip(skipCount).Take(takeCount);
  return Json( new { items = model, count = model.Count() }, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)

脚本

var skipCount = 5; // start at 6th record (assumes first 5 included in initial view)
var takeCount = 5; // return new 5 records
var hasMoreRecords = true;

function FetchDataFromServer() {
  if (!hasMoreRecords) {
    return;
  }
  $.ajax({
    url: '@Url.Action("FetchData")',
    data: { skipCount : skipCount, takeCount: takeCount },
    datatype: 'json',
    success: function (data) {
      if (data.Count < 5) {
        hasMoreRecords = false; // signal no more records to display
      }
      $.each(data.items, function(index, item) {
        // assume each object contains ID and Name properties
        var container = $('<div></div>');
        var id = $('<div></div>').text($(this).ID);
        container.append(id);
        var name = $('<div></div>').text($(this).Name);
        container.append(name);
        $("#result").append(container);
      });
      skipCount += takeCount; // update for next iteration
    },
    error: function () {
      alert("error");
    }
  });
}
Run Code Online (Sandbox Code Playgroud)