使用LINQ获取有关点击加载更多按钮的数据,从开始索引到结束索引

时间:2017-01-02 13:19:11

标签: c# sql linq

我正在使用WEBAPI和LINQ从SQL获取数据,我的页面中有一个Load More按钮,当我点击时 - 它每次应该带20条记录,我正在使用Start Index和End Index有我的表列ID是主键。

当我点击加载更多按钮时,我将从列表视图中获取最后一个ID即最后一个数据ID,我正在使用该Id具有我的开始索引,现在我的问题是当我点击加载更多它带来了20个数据但以前的数据被删除了。当我第一次点击加载更多按钮时,我需要获得20 + 20 = 40个数据。

如何实现这一目标?请在下面找到我的代码

          //Linq//
          public dynamic getALlImage(int userID, int LastImageID)
   {
       //here LastImageId is my Listview Last data ImageID
         if  (LastImageID!=0)
          {
   startIndex= LastImageID+20;
   EndIndex=LastImageID-20;
 }
   else
 {
  startIndex= 20;
   EndIndex=0;  
 }

  GetAllListFields = (from user in dbContext.UserTable
                  join img in dbContext.ImageTable on user.userId equals img.userID into UserVoice
                  from Image in UserVoice.DefaultIfEmpty()
                  where (Image.imgId <= startIndex && Image.imgId >= EndIndex && Image.userID == userID && Image.IsStatusChecked == false)
                  orderby feeds.CreatedAt descending
                  select new
                  {
                       PostImage = Image == null ? 0 : Image.ImageID,
                      Image = usrMessae==null?0: Image.userMessage,
                      createdAt = feeds == null ? DateTime.UtcNow : feeds.CreatedAt,
                      UserId = userdata == null ? 0 : userdata.userId,
                      Name = userdata == null ? " " : userdata.Name,
                      username = userdata == null ? " " : userdata.userName,
               Email = userdata == null ? " " : userdata.emailID,
                    }).Distinct().OrderByDescending(x => x.createdAt).Take(startIndex).ToList();
     } 


   //HTML//
   <div data-role="view"> 
   <div>
   <ul  data-role="listview"   data-bind="foreach:ImageList">
   <li>
   <div data-bind="text:userID"></div>
   <div data-bind="text:userName"></div>
   <div data-bind="text:UserImageSrc"></div>
   <div data-bind="text:ImageId></div>
   <div data-bind="text:PostImageSrc"></div>
   <div >
   <button data-bind="click:getLoadMoreImageData">Load More</button>
  </div>
  </li>
  </ul>
  </div>
  </div>

      //Knockout Js
  function UserImageViewModel()
    {
        self=this;
      var lastModelValue = (self.FeedPostdata()      [self.FeedPostdata().length - 1]);
      var lastPostImageId = (lastModelValue.ImageId())

     //On Load More click  last Image ID      
     self.getLoadMoreImageData=function()
     {     
       $('#loading').show();
       var UserModel = { userID:userID,ImageId:lastPostImageId}
       jQuery.support.cors = true;
       $.ajax({
        type: "POST",
        dataType: "json",
        url: serverUrl + 'api/xxx/xxx',
        data: UserModel,
        success: function (data) {
        self.ImageList($.map(data, function (item) {
                return new ImageModel(item);
        }));},
        complete: function () {
            $('#loading').hide();
        },
        error: function (error) {
            jsonValue = jQuery.parseJSON(error.responseText);} });}}


    // On Page Load  without last Image ID  
      self.getALlUSerImage=function()
     {

        $('#loading').show();
        var UserModel = { userID: userID}
        jQuery.support.cors = true;
        $.ajax({
        type: "POST",
        dataType: "json",
        url: serverUrl + 'api/xxx/xxx',
        data: UserModel,
        success: function (data) {
        self.ImageList($.map(data, function (item) {
                return new ImageModel(item);
        }));},
        complete: function () {
            $('#loading').hide();
        },
        error: function (error) {
            jsonValue = jQuery.parseJSON(error.responseText);} });
            }}
            }


           //Model//
          function ImageModel(item)
          {
            self.userId=ko.observable(item.userId);
            self.userName=ko.observable(item.UserName);
            self.UserImageSrc = ko.computed(function () {
            return "data:" + self.UserImageSrcType() + ";base64," + self.UserImageSrcBase64();});   

            self.ImageID=ko.observable(item.ImageId)
            self.PostImageSrc = ko.computed(function () {
            return "data:" + self.PostImageType() + ";base64," +  self.PostImageBase64();});                      
          }

       $(document).ready(function () {
       ViewModel = new UserImageViewModel();
       ko.applyBindings(ViewModel);
       }

1 个答案:

答案 0 :(得分:1)

您可以声明一个变量并为其分配初始数据。每次成功调用ajax后,都会对该变量进行响应,并将该变量的值传递给您的模型。