廢話不多說了,直接給大家貼程式碼了。
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 id="Index">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异步方式到达这里~~"; 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文件,從控制器傳回的強型別部分視圖內容呈現到UpdateTargetId指定的div。
@{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2 id="Index">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到达这里~~"; 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中實作部分內容非同步載入
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>

JavaScript核心數據類型在瀏覽器和Node.js中一致,但處理方式和額外類型有所不同。 1)全局對像在瀏覽器中為window,在Node.js中為global。 2)Node.js獨有Buffer對象,用於處理二進制數據。 3)性能和時間處理在兩者間也有差異,需根據環境調整代碼。

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python和JavaScript的主要區別在於類型系統和應用場景。 1.Python使用動態類型,適合科學計算和數據分析。 2.JavaScript採用弱類型,廣泛用於前端和全棧開發。兩者在異步編程和性能優化上各有優勢,選擇時應根據項目需求決定。

選擇Python還是JavaScript取決於項目類型:1)數據科學和自動化任務選擇Python;2)前端和全棧開發選擇JavaScript。 Python因其在數據處理和自動化方面的強大庫而備受青睞,而JavaScript則因其在網頁交互和全棧開發中的優勢而不可或缺。

Python和JavaScript各有優勢,選擇取決於項目需求和個人偏好。 1.Python易學,語法簡潔,適用於數據科學和後端開發,但執行速度較慢。 2.JavaScript在前端開發中無處不在,異步編程能力強,Node.js使其適用於全棧開發,但語法可能複雜且易出錯。

javascriptisnotbuiltoncorc; sanInterpretedlanguagethatrunsonenginesoftenwritteninc.1)JavascriptwasdesignedAsignedAsalightWeight,drackendedlanguageforwebbrowsers.2)Enginesevolvedfromsimpleterterpretpretpretpretpreterterpretpretpretpretpretpretpretpretpretcompilerers,典型地,替代品。

JavaScript可用於前端和後端開發。前端通過DOM操作增強用戶體驗,後端通過Node.js處理服務器任務。 1.前端示例:改變網頁文本內容。 2.後端示例:創建Node.js服務器。

選擇Python還是JavaScript應基於職業發展、學習曲線和生態系統:1)職業發展:Python適合數據科學和後端開發,JavaScript適合前端和全棧開發。 2)學習曲線:Python語法簡潔,適合初學者;JavaScript語法靈活。 3)生態系統:Python有豐富的科學計算庫,JavaScript有強大的前端框架。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3漢化版
中文版,非常好用

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3 Linux新版
SublimeText3 Linux最新版

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。