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>