MVC中实现部分内容异步加载

action中定义一个得到结果集的方法
   

 public ActionResult GetItemTree(string title, int itemid, int? page)
        {
            pp = new PagingParam(page ?? 1, VConfig.WebConstConfig.PageSize);
            Common.Page.PagedList<Entity.Res_Item_Resource_R> res_Item_Resource_R = iResourceService.GetRes_Item_Resource_RByItemId(itemid, pp);
            ViewData["res_Item_Resource_R"] = res_Item_Resource_R;
            res_Item_Resource_R.AddParameters = new System.Collections.Specialized.NameValueCollection();
            res_Item_Resource_R.AddParameters.Add("title", title);
            res_Item_Resource_R.AddParameters.Add("itemid", itemid.ToString());

            ViewResult vr = new ViewResult
            {
                ViewData = ViewData,
                MasterName = "",
            };
            return vr;
        }


    在主页面使用下面jquery代码异步调用上面的action
      

  $(function () {
        var id = '<%=itemid %>';
        $.ajax({
            type: "POST",
            url: "/Student/GetItemTree",
            data: { title: '<%=Model.Name %>', itemid: id, page: 1 },
            beforeSend: function (data) { //取回数据前
                $("#itemTree").html('<span style="padding:5">数据加载中...</span>');
            },
            error: function (data) { //发生错误时
//                debugger;
            },
            success: function (data) { //成功返回时
                $("#itemTree").html(data);
            }
        });


   最后在分部视图GetItemTree.ascx中写上你要返回的数据结构即可
   注意一点就是,如果涉及到分页,要用AJAX分页方式

   <div style="float: left">
        <%=Html.AjaxPager(resItemResourceBefore, "itemTree", "GetItemTree", "Student")%>
    </div>

 

posted @ 2011-11-04 10:20  张占岭  阅读(4202)  评论(0编辑  收藏  举报