MVC3 獲取JSON對象

  1. 1.建一個mvc3的項目,取名叫MVC3Test(事先安裝mvc3 version:1.13.113.0)  
  2.   
  3. 2.修改About.cshtml,如下代碼  
  4. About.cshtml  
  5. @{  
  6.     ViewBag.Title = "About Us";  
  7. }  
  8. <script type="text/javascript">  
  9.     $(function () {  
  10.         $.ajax({  
  11.             url: "/Home/GetSchools",  
  12.             type: "POST",  
  13.             contentType: "application/json; charset=utf-8",  
  14.             data: "{}",  
  15.             dataType: "json",  
  16.             success: function (data) {  
  17.                 $("#sltSchool").empty();  
  18.                 $("#sltSchool").html(data);  
  19.             },  
  20.             error: function ErrorCallback(XMLHttpRequest, textStatus, errorThrown) {  
  21.                 alert(errorThrown + ":" + textStatus);  
  22.             }  
  23.         });  
  24.         // Get the departments depend on the school  
  25.         $("#sltSchool").change(function () {  
  26.             GetDepartments($("#sltSchool").val());  
  27.         });  
  28.     });  
  29.   
  30.     function GetDepartments(sId) {  
  31.         $.ajax({  
  32.             url: "/Home/GetSecondCatalog",  
  33.             type: "POST",  
  34.             contentType: "application/json; charset=utf-8",  
  35.             data: "{schoolId:"+sId+"}",  
  36.             dataType: "json",  
  37.             success: function (data) {  
  38.                 $('#sltDepartment').empty();  
  39.                 $('#sltDepartment').html(data);  
  40.             },  
  41.             error: function ErrorCallback(XMLHttpRequest, textStatus, errorThrown) {  
  42.                 alert(errorThrown + ":" + textStatus);  
  43.             }  
  44.         });  
  45.     }  
  46. </script>  
  47. <div>  
  48.     <h2>  
  49.         About</h2>  
  50.     <p>  
  51.         Put content here.  
  52.     </p>  
  53.     <div>  
  54.         <span>  
  55.             <label>  
  56.                 School :  
  57.             </label>  
  58.             <select id="sltSchool">  
  59.                 <option value="-1">select...</option>  
  60.             </select></span> <span style="margin-left: 50px">  
  61.                 <label>  
  62.                     Department :</label>  
  63.                 <select id="sltDepartment">  
  64.                     <option value="-1">select...</option>  
  65.                 </select>  
  66.             </span>  
  67.     </div>  
  68. </div>  
  69. 3.創建幾個model  
  70.  (1) TestSchool.cs  
  71. TestSchool  
  72. using System;  
  73. using System.Collections.Generic;  
  74. using System.Linq;  
  75. using System.Web;  
  76.   
  77. namespace MVC3Test.Models  
  78. {  
  79.     public class TestSchool  
  80.     {  
  81.         public int Id { get; set; }  
  82.         public string Name { get; set; }  
  83.     }  
  84. }  
  85.  (2) TestSchoolDepartment.cs  
  86. TestSchoolDepartment.cs  
  87. using System;  
  88. using System.Collections.Generic;  
  89. using System.Linq;  
  90. using System.Web;  
  91.   
  92. namespace MVC3Test.Models  
  93. {  
  94.     public class TestSchoolDepartment  
  95.     {  
  96.         public int Id { get; set; }  
  97.         public int SchoolId { get; set; }  
  98.         public string Name { get; set; }  
  99.     }  
  100. }  
  101.  (3) TestModels.cs  
  102. TestModels.cs  
  103. using System;  
  104. using System.Collections.Generic;  
  105. using System.Linq;  
  106. using System.Web;  
  107.   
  108. namespace MVC3Test.Models  
  109. {  
  110.     public class TestModels  
  111.     {  
  112.         public static List<TestSchool> GetAllSchools()  
  113.         {  
  114.             return new List<TestSchool>()  
  115.             {  
  116.                 new TestSchool{Id=1,Name="ABC"},  
  117.                 new TestSchool{Id=2,Name="DEF"},  
  118.                 new TestSchool{Id=3,Name="HIJ"},  
  119.                 new TestSchool{Id=4,Name="LMN"}  
  120.             };  
  121.         }  
  122.   
  123.         public static List<TestSchoolDepartment> GetAllDepartment()  
  124.         {  
  125.             return new List<TestSchoolDepartment>()  
  126.             {  
  127.                 new TestSchoolDepartment{Id=1,SchoolId=1,Name="ABC_D1"},  
  128.                 new TestSchoolDepartment{Id=2,SchoolId=1,Name="ABC_D2"},  
  129.                 new TestSchoolDepartment{Id=3,SchoolId=1,Name="ABC_D3"},  
  130.                 new TestSchoolDepartment{Id=4,SchoolId=2,Name="DEF_D1"},  
  131.                 new TestSchoolDepartment{Id=5,SchoolId=2,Name="DEF_D2"},  
  132.                 new TestSchoolDepartment{Id=6,SchoolId=3,Name="HIJ_D1"},  
  133.                 new TestSchoolDepartment{Id=7,SchoolId=3,Name="HIJ_D2"},  
  134.                 new TestSchoolDepartment{Id=8,SchoolId=3,Name="HIJ_D3"},  
  135.                 new TestSchoolDepartment{Id=9,SchoolId=3,Name="HIJ_D4"},  
  136.                 new TestSchoolDepartment{Id=10,SchoolId=4,Name="LMN_D1"}  
  137.             };  
  138.         }  
  139.  public static List<TestSchoolDepartment> GetDepartmentBySchoolId(int schoolId)  
  140.         {  
  141. List<TestSchoolDepartment> testSchoolDepartment = new List<TestSchoolDepartment>();  
  142. foreach (TestSchoolDepartment department in GetAllDepartment())  
  143.             {  
  144.                 if (department.SchoolId == schoolId)  
  145.                 {  
  146.                     testSchoolDepartment.Add(department);  
  147.                 }  
  148.             }  
  149.             return testSchoolDepartment;  
  150.         }  
  151.     }  
  152. }  
  153. 4.由於About是在Home頁面裏的,所以它的controller應該在HomeController裏,我們添加兩個controller,如下:  
  154. using System;  
  155. using System.Collections.Generic;  
  156. using System.Linq;  
  157. using System.Web;  
  158. using System.Web.Mvc;  
  159. using MVC3Test.Models;  
  160. using System.Text;  
  161.   
  162. namespace MVC3Test.Controllers  
  163. {  
  164.     public class HomeController : Controller  
  165.     {  
  166.         public ActionResult Index()  
  167.         {  
  168.             ViewBag.Message = "Welcome to ASP.NET MVC!";  
  169.   
  170.             return View();  
  171.         }  
  172.   
  173.         public ActionResult About()  
  174.         {  
  175.             return View();  
  176.         }  
  177.   
  178.         [HttpPost]  
  179.         public JsonResult GetSchools()  
  180.         {  
  181.             StringBuilder sb = new StringBuilder();  
  182.             sb.Append("<option value=\"-1\">select...</option>");  
  183.             foreach (var item in TestModels.GetAllSchools())  
  184.             {  
  185. sb.AppendFormat("<option value=\"{0}\">{1}</option>", item.Id, item.Name);  
  186.             }  
  187.             string result = sb.ToString();  
  188.             return this.Json(result, JsonRequestBehavior.AllowGet);  
  189.         }  
  190.   
  191.         [HttpPost]  
  192.         public JsonResult GetSecondCatalog(int schoolId)  
  193.         {  
  194.             StringBuilder sb = new StringBuilder();  
  195.             sb.Append("<option value=\"-1\">select...</option>");  
  196.             foreach (var item in TestModels.GetDepartmentBySchoolId(schoolId))  
  197.             {  
  198. sb.AppendFormat("<option value=\"{0}\">{1}</option>", item.Id, item.Name);  
  199.             }  
  200.             string result = sb.ToString();  
  201.             return this.Json(result, JsonRequestBehavior.AllowGet);  
  202.         }  
  203.     }  
  204. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章