ASP.NET MVC通過(guò)勾選checkbox更改select的內(nèi)容
遇到了這樣的一個(gè)需求:通過(guò)勾選checkbox來(lái)更改select的內(nèi)容。
在沒(méi)有勾選checkbox之前是這樣的:
在勾選checkbox之后是這樣的:
想通過(guò)ajax異步來(lái)實(shí)現(xiàn)。所以,從控制器拿到的json數(shù)據(jù),在控制器中應(yīng)該先是Dictionary<string, string>類(lèi)型,然后再轉(zhuǎn)換成json格式。
在沒(méi)有勾選checkbox之前,select中內(nèi)容對(duì)應(yīng)的Model為:
public class Old {public int Id { get; set; }public string Name { get; set; } }
在勾選checkbox之后,select中內(nèi)容對(duì)應(yīng)的Model為:
public class NewItem {public int Id { get; set; }public string Name { get; set; } }
Home控制器中應(yīng)該給出對(duì)應(yīng)的json數(shù)據(jù)。
public class HomeController : Controller {public ActionResult Index(){ return View();}public ActionResult GetOld(){ var olds = new List<Old> {new Old(){Id = 1, Name = "老版本1"},new Old(){Id = 2, Name = "老版本2"},new Old(){Id = 3, Name = "老版本3"} }; IDictionary<string, string> result = new Dictionary<string, string> {{"-1","None"}}; foreach (var item in olds) {result.Add(item.Id.ToString(), item.Name); } return Json(result, JsonRequestBehavior.AllowGet);}public ActionResult GetNew(){ var news = new List<NewItem> {new NewItem(){Id = 1, Name = "新版本1"},new NewItem(){Id = 2, Name = "新版本2"} }; IDictionary<string, string> result = new Dictionary<string, string> { { "-1", "None" } }; foreach (var item in news) {result.Add(item.Id.ToString(), item.Name); } return Json(result, JsonRequestBehavior.AllowGet);} }
在Home/Index.cshtml視圖中,根據(jù)checkbox是否勾選來(lái)呈現(xiàn)不同的內(nèi)容。
@{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml";}<h2>Index</h2><div> <select id="v"></select></div><div> <span>是否選擇新版本:</span><input type="checkbox" id="cn"/></div>@section scripts{ <script type="text/javascript">$(function () { //初始獲取老版本 getOldOnes(); //勾選checkbox事件 $("#cn").on("change", function() {if ($(this).is(":checked")) { getNewOnes();} else { getOldOnes();} });});//獲取老版本function getOldOnes() { $.getJSON("@Url.Action("GetOld","Home")", function(data) {var $s = $("#v");$s.children().remove();$.each(data, function(key, value) { $s.append("<option value="" + key + "">" + value + "</option>");});$s.effect("shake", { times: 4 }, 100); });}//獲取新版本function getNewOnes() { $.getJSON("@Url.Action("GetNew","Home")", function (data) {var $s = $("#v");$s.children().remove();$.each(data, function (key, value) { $s.append("<option value="" + key + "">" + value + "</option>");});$s.effect("shake", { times: 4 }, 100); });} </script>}
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章:
1. vue中關(guān)于checkbox使用的問(wèn)題2. Ant design vue table 單擊行選中 勾選checkbox教程3. ASP.NET MVC前臺(tái)動(dòng)態(tài)添加文本框并在后臺(tái)使用FormCollection接收值4. python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5復(fù)選框控件QCheckBox詳細(xì)使用方法與實(shí)例5. ASP.NET MVC使用異步Action的方法6. 解決Django中checkbox復(fù)選框的傳值問(wèn)題7. ASP.NET MVC使用JSAjaxFileUploader插件實(shí)現(xiàn)單文件上傳8. 使用EF Code First搭建簡(jiǎn)易ASP.NET MVC網(wǎng)站并允許數(shù)據(jù)庫(kù)遷移9. ASP.NET MVC實(shí)現(xiàn)單個(gè)圖片上傳、限制圖片格式與大小并在服務(wù)端裁剪圖片10. ASP.Net MVC利用NPOI導(dǎo)入導(dǎo)出Excel的示例代碼
