如何在MVC中使用Take和Skip功能

时间:2015-02-26 05:45:50

标签: linq asp.net-mvc-4 razor asp.net-mvc-5 asp.net-ajax

我想加载数据并跳过之前已在视图中附加的数据,使用跳过并在mvc中取计数。这是我的视图,我在哪里获取数据

var skipCount = 5; 
var takeCount = 5; 

function loadMore() {
  $(window).bind('scroll', bindScroll);
  itemCount = takeCount;
  skipCount += takeCount;
  setTimeout(function () {
    getFeed();
  },100);            
}

function bindScroll() {
  if ($(window).scrollLeft() + $(window).width() > $('.tile-area').width() - 130) {
    $(window).unbind('scroll');
    loadMore();
  }
}

function getFeed() {             
  $.ajax({
    type: "Post",
    url: "/PlanetFeed/PlanetfeedPartial",
    dataType: "html",
    data: { id: planetFeedOwnerId, filterType: filterType, taggedItemGraphId: taggedItemGraphId, itemCount: takeCount, SkipCount: skipCount },      //"feedGraphId=10696",
    success: function (data) {
    if (data === null) {

    } else {
      $('.tile-area-title').html("");
      var div = $('.planetFeed:last');
      div.after(data);
      skipCount += takeCount + 1;
    }                                
  });
}

这是我的控制器,我传递参数

public ActionResult PlanetfeedPartial(Guid id, string filterType, Guid taggedItemGraphId, int itemCount, int SkipCount)
{
  var planetfeedsOrder = from p in db.PlanetFeeds
                         where p.CurrentState != 1
                         join s in searchTag on p.PlanetFeedGraphId equals s.SecondaryTaggedGraphItemId
                         join g in db.Graphs on p.PlanetFeedItemGraphId equals g.GraphID
                         join u in db.UserInfos on p.PlanetFeedPosterId equals u.UserInfoID
                         orderby p.PostDate descending
                         select new PlanetFeedViewModel
                         {            
                           Username = u.FirstName + " " + u.LastName,                                          
                           isRootFeed = p.isRootFeed,
                           PostDate = p.PostDate,
                           CommentCount = g.CountResponses,
                           CountPositiveReactions = g.CountPositiveReactions,
                           CountNegativeReactions = g.CountNegativeReactions,
                           ItemID = g.ItemID,
                           UserLevel = u.UserLevel,
                           CurrentState = p.CurrentState,
                           Address = g.Address
                         };
  return PartialView("_PlanetfeedPartial", planetfeedsOrder.OrderByDescending(p => p.PostDate).Skip(SkipCount).Take(itemCount).ToList());
}

我没有得到正确的数据,每次当我在滚动中加载数据时,得到的数据不正确并且所有数据都没有加载

1 个答案:

答案 0 :(得分:0)

var planetfeedsOrder = (from p in db.PlanetFeeds
                         where p.CurrentState != 1
                         join s in searchTag on p.PlanetFeedGraphId equals s.SecondaryTaggedGraphItemId
                         join g in db.Graphs on p.PlanetFeedItemGraphId equals g.GraphID
                         join u in db.UserInfos on p.PlanetFeedPosterId equals u.UserInfoID
                         orderby p.PostDate descending
                         select new PlanetFeedViewModel
                         {            
                           Username = u.FirstName + " " + u.LastName,                                          
                           isRootFeed = p.isRootFeed,
                           PostDate = p.PostDate,
                           CommentCount = g.CountResponses,
                           CountPositiveReactions = g.CountPositiveReactions,
                           CountNegativeReactions = g.CountNegativeReactions,
                           ItemID = g.ItemID,
                           UserLevel = u.UserLevel,
                           CurrentState = p.CurrentState,
                           Address = g.Address
                         }).orderby(o=>o.id).skip(100);

跳过100然后显示您的数据,并按您想要序列数据的任何ID或字符串创建顺序。