国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > AJAX > 正文

MVC Ajax Helper或Jquery異步加載部分視圖

2024-09-01 08:33:05
字體:
供稿:網(wǎng)友
這篇文章主要介紹了MVC Ajax Helper或Jquery異步加載部分視圖的相關(guān)資料,需要的朋友可以參考下
 

廢話不多說了,直接給大家貼代碼了。

Model:

namespace MvcApplication1.Models{ public class Team {  public string Preletter { get; set; }  public string Name { get; set; } }}

通過jQuery異步加載部分視圖

Home/Index.cshtml視圖中:

@{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml";}<h2>Index</h2><div> <a href="#" id="a">通過jQuery異步</a> <br/></div><div id="result"></div>@section scripts{ <script type="text/javascript">  $(function() {   $('#a').click(function() {    $.ajax({     url: '@Url.Action("Index","Home")',     data: { pre: 'B' },     type: 'POST',     success: function(data) {      $('#result').empty().append(data);     }    });    return false;   });  }); </script>}

HomeController控制器中:

using System.Collections.Generic;using System.Linq;using System.Web.Mvc;using MvcApplication1.Models;namespace MvcApplication1.Controllers{ public class HomeController : Controller {  public ActionResult Index()  {   return View();  }  [HttpPost]  public ActionResult Index(string pre)  {   var result = GetAllTeams().Where(t => t.Preletter == pre).ToList();   ViewBag.msg = "通過jQuery異步方式到達(dá)這里~~";   return PartialView("TeamY", result);  }  private List<Team> GetAllTeams()  {   return new List<Team>()   {    new Team(){Name = "巴西隊", Preletter = "B"},    new Team(){Name = "克羅地亞隊", Preletter = "K"},    new Team(){Name = "巴拉圭", Preletter = "B"},    new Team(){Name = "韓國", Preletter = "K"}   };  } }}

部分視圖TeamY.cshtml:

@model IEnumerable<MvcApplication1.Models.Team> @{ var result = string.Empty; foreach (var item in Model) {  result += item.Name + ","; }}@ViewBag.msg.ToString()<br/>@result.Substring(0,result.Length - 1)

通過MVC Ajax Helper異步加載部分視圖

Home/Index.cshtml視圖中需要引用jquery.unobtrusive-ajax.js文件,從控制器返回的強(qiáng)類型部分視圖內(nèi)容呈現(xiàn)到UpdateTargetId指定的div中。

@{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml";}<h2>Index</h2><div> @Ajax.ActionLink("通過MVC Ajax Helper","Load","Home", new {pre = "K"}, new AjaxOptions(){UpdateTargetId = "result1"})</div><div id="result1"></div>

HomeController控制器中:

using System.Collections.Generic;using System.Linq;using System.Web.Mvc;using MvcApplication1.Models;namespace MvcApplication1.Controllers{ public class HomeController : Controller {  public ActionResult Index()  {   return View();  }  public ActionResult Load(string pre)  {   var result = GetAllTeams().Where(t => t.Preletter == pre).ToList();   ViewBag.msg = "通過MVC Ajax Helper到達(dá)這里~~";   return PartialView("TeamY", result);  }  private List<Team> GetAllTeams()  {   return new List<Team>()   {    new Team(){Name = "巴西隊", Preletter = "B"},    new Team(){Name = "克羅地亞隊", Preletter = "K"},    new Team(){Name = "巴拉圭", Preletter = "B"},    new Team(){Name = "韓國", Preletter = "K"}   };  } }}

部分視圖和上一種方式一樣。

頁面刷新的方式加載部分視圖方法包括:

Html.RenderPartial()
Html.RenderAction()

下面給大家介紹MVC中實現(xiàn)部分內(nèi)容異步加載

action中定義一個得到結(jié)果集的方法

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代碼異步調(diào)用上面的action

 $(function () {  var id = '<%=itemid %>';  $.ajax({   type: "POST",   url: "/Student/GetItemTree",   data: { title: '<%=Model.Name %>', itemid: id, page: 1 },   beforeSend: function (data) { //取回數(shù)據(jù)前    $("#itemTree").html('<span style="padding:5">數(shù)據(jù)加載中...</span>');   },   error: function (data) { //發(fā)生錯誤時//    debugger;   },   success: function (data) { //成功返回時    $("#itemTree").html(data);   }  });

   最后在分部視圖GetItemTree.ascx中寫上你要返回的數(shù)據(jù)結(jié)構(gòu)即可
   注意一點就是,如果涉及到分頁,要用AJAX分頁方式

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


注:相關(guān)教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 迁安市| 徐州市| 双牌县| 望谟县| 彰武县| 牙克石市| 大名县| 平利县| 潮安县| 昭苏县| 崇阳县| 改则县| 华阴市| 濉溪县| 尉氏县| 深州市| 湘西| 桑日县| 东莞市| 合肥市| 柳林县| 中卫市| 江西省| 新泰市| 西青区| 嘉禾县| 弥勒县| 延庆县| 祁东县| 仙游县| 洪湖市| 新邵县| 桦川县| 乌兰察布市| 舟曲县| 台北县| 象州县| 新河县| 新田县| 普格县| 乌鲁木齐市|