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 = "巴西隊", 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文件,從控制器返回的強類型部分視圖內(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)文章
Jquery幻燈片特效代碼分享 打開頁面隨機選擇切換方式(3)
jQuery是一個非常優(yōu)秀的 JavaScript 框架,使用簡單靈活,一個漂亮的幻燈片更能吸引訪客的注意力。本文實例講述了jQuery實現(xiàn)時尚漂亮的幻燈片特效,基本能滿足你在網(wǎng)頁上使用幻燈片(焦點圖)效果。分享給大家供大家參考。具體如下:2015-08-08
jQuery.cookie.js實現(xiàn)記錄最近瀏覽過的商品功能示例
這篇文章主要介紹了jQuery.cookie.js實現(xiàn)記錄最近瀏覽過的商品功能,結(jié)合實例形式分析了基于jQuery.cookie.js插件創(chuàng)建cookie及保存瀏覽記錄的操作技巧,需要的朋友可以參考下2017-01-01
js調(diào)用iframe實現(xiàn)打印頁面內(nèi)容的方法
這篇文章主要介紹了js調(diào)用iframe實現(xiàn)打印頁面內(nèi)容的方法,需要的朋友可以參考下2014-03-03

