MVC+JSON 无限滚动翻页_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > MVC+JSON 无限滚动翻页

MVC+JSON 无限滚动翻页

 2014/4/28 13:27:41  chr1219  博客园  我要评论(0)
  • 摘要:///实体publicpartialclassJsonModel{publicintID{get;set;}publicintTitle{get;set;}}///控制器返回JSON格式化JSON我用了第三方的:Newtonsoft.Json似乎现在很多人都在用publicstringList(intpage=1){Response.ContentType="application/Json";List<News>J=db.News.OrderByDescending(p=>
  • 标签:MVC JSON JS
///实体
public
partial class JsonModel { public int ID{ get; set; } public int Title{ get; set; } }

 

///控制器 返回JSON 格式化JSON我用了第三方的:Newtonsoft.Json 似乎现在很多人都在用
public string List(int page=1)
{
  Response.ContentType = "application/Json";   List
<News> J= db.News.OrderByDescending(p => p.ID).Skip(10 * page).Take(10).ToList();   return JsonConvert.SerializeObject(J); }

 

<div class="content">
    <ul class="list" id="container">
    </ul>
    <div style="text-align:center;padding:10px;" id="load">点击显示数据</div>
</div>

 

javascript 需要JQ支付

    var i = 1;
    function JsonLoad() {
        LoadJsonDate(i);
    }

    function LoadJsonDate(pageindex) {
        i++;
        $.get("/json/List", { page: pageindex}, function (data) {
            if (data.length > 0) {
                var jsonObj = JSON.parse(data);
                AddDate(jsonObj);
            } else {
                $("#load").html("没有了...");
            }
        });
    }

    function AddDate(jsondate) {
        var html = '';
        for (var j = 0; j < jsondate.length; j++) {
            html += "<li><a href=\"View/" + jsondate[j].ID + "\">" + jsondate[j].title + "</a></li>";
        }
        $("#container").append(html);

        if (j < 10) {
            $("#load").html("没有了...");
        }
    }


    $(function () {

        JsonLoad();//打开页面就加载


        //点击底部加载区就加载//为防止某些低版本手机滚动条事件不稳定
        $("#load").click(function () {
            $("#load").html("<img src=\"load.gif\" alt=\"正在加载\"/>");
            setTimeout("JsonLoad()", 500);//延时加载
        });


       
        var winH = $(window).height();
        var scrollHandler = function () {
            var pageH = $(document.body).height();
            var scrollT = $(window).scrollTop();
            var h = (pageH - winH - scrollT) / winH;
            $("#load").html("点击显示下10条");
            if (h <= 0) {
                $("#load").html("<img src=\"load.gif\" alt=\"正在加载\"/>");
                setTimeout("JsonLoad()", 500);//延时加载
            }
        }

        $(window).scroll(scrollHandler);

    });

 

 

 

 

上一篇: C# 中的 == 和 equals()有什么区别? 下一篇: 没有下一篇了!
发表评论
用户名: 匿名