MVC Ajax Helper或Jquery異步加載部分視圖
廢話不多說了,直接給大家貼代碼了。
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 = "巴西隊(duì)", Preletter = "B"},
new Team(){Name = "克羅地亞隊(duì)", Preletter = "K"},
new Team(){Name = "巴拉圭", Preletter = "B"},
new Team(){Name = "韓國(guó)", 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 = "巴西隊(duì)", Preletter = "B"},
new Team(){Name = "克羅地亞隊(duì)", Preletter = "K"},
new Team(){Name = "巴拉圭", Preletter = "B"},
new Team(){Name = "韓國(guó)", Preletter = "K"}
};
}
}
}
部分視圖和上一種方式一樣。
頁(yè)面刷新的方式加載部分視圖方法包括:
Html.RenderPartial()
Html.RenderAction()
下面給大家介紹MVC中實(shí)現(xiàn)部分內(nèi)容異步加載
action中定義一個(gè)得到結(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;
}
在主頁(yè)面使用下面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) { //取回?cái)?shù)據(jù)前
$("#itemTree").html('<span style="padding:5">數(shù)據(jù)加載中...</span>');
},
error: function (data) { //發(fā)生錯(cuò)誤時(shí)
// debugger;
},
success: function (data) { //成功返回時(shí)
$("#itemTree").html(data);
}
});
最后在分部視圖GetItemTree.ascx中寫上你要返回的數(shù)據(jù)結(jié)構(gòu)即可
注意一點(diǎn)就是,如果涉及到分頁(yè),要用AJAX分頁(yè)方式
<div style="float: left"> <%=Html.AjaxPager(resItemResourceBefore, "itemTree", "GetItemTree", "Student")%> </div>
相關(guān)文章
jquery實(shí)現(xiàn)用戶登陸界面(示例講解)
下面小編就為大家?guī)硪黄猨query實(shí)現(xiàn)用戶登陸界面(示例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
jquery實(shí)現(xiàn)一個(gè)簡(jiǎn)單的表單驗(yàn)證實(shí)例
下面小編就為大家?guī)硪黄猨query實(shí)現(xiàn)一個(gè)簡(jiǎn)單的表單驗(yàn)證實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-03-03
Jquery幻燈片特效代碼分享 打開頁(yè)面隨機(jī)選擇切換方式(3)
jQuery是一個(gè)非常優(yōu)秀的 JavaScript 框架,使用簡(jiǎn)單靈活,一個(gè)漂亮的幻燈片更能吸引訪客的注意力。本文實(shí)例講述了jQuery實(shí)現(xiàn)時(shí)尚漂亮的幻燈片特效,基本能滿足你在網(wǎng)頁(yè)上使用幻燈片(焦點(diǎn)圖)效果。分享給大家供大家參考。具體如下:2015-08-08
jquery獲取自定義屬性(attr和prop)實(shí)例介紹
jquery中用attr()方法來獲取和設(shè)置元素屬性,attr是attribute(屬性)的縮寫,在jQuery DOM操作中會(huì)經(jīng)常用到attr(),attr()有4個(gè)表達(dá)式2013-04-04
jQuery.cookie.js實(shí)現(xiàn)記錄最近瀏覽過的商品功能示例
這篇文章主要介紹了jQuery.cookie.js實(shí)現(xiàn)記錄最近瀏覽過的商品功能,結(jié)合實(shí)例形式分析了基于jQuery.cookie.js插件創(chuàng)建cookie及保存瀏覽記錄的操作技巧,需要的朋友可以參考下2017-01-01
jQuery滾動(dòng)條插件nanoscroller使用指南
本文給大家介紹的nanoScrollerJS是一款使用簡(jiǎn)單方式實(shí)現(xiàn) Mac OS X Lion 系統(tǒng)滾動(dòng)條效果的jQuery插件。該滾動(dòng)條插件利用原生的滾動(dòng)條可以工作在 iPad、iPhone 和一些 Android Tablets上。2015-04-04
js調(diào)用iframe實(shí)現(xiàn)打印頁(yè)面內(nèi)容的方法
這篇文章主要介紹了js調(diào)用iframe實(shí)現(xiàn)打印頁(yè)面內(nèi)容的方法,需要的朋友可以參考下2014-03-03

