ASP.NET MVC 中使用用戶控件

週末加班,閒來無事,寫篇博客,講講怎麼在 ASP.NET MVC2中使用用戶控件。首先咱們新建一個用戶控件,javascript

咱們命名爲SelectGroup.ascx,代碼以下java

  
  
  
  
  1. <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> 
  2. <script language="javascript" type="text/javascript" src="<%=Url.Content("~/Areas/Util/Scripts/SelectGroup.js") %>"></script> 
  3. <div> 
  4.     <table> 
  5.         <tr> 
  6.             <td style="text-align:right"> 
  7.                 招生批次  
  8.             </td> 
  9.             <td> 
  10.                 <select id="admissionBatch" style="width: 150px"> 
  11.                 </select> 
  12.             </td> 
  13.             <td style="text-align:right; width:80px"> 
  14.                 學歷層次  
  15.             </td> 
  16.             <td> 
  17.                 <select id="edcuationLevel" style="width: 150px"> 
  18.                 </select> 
  19.             </td> 
  20.             <td style="text-align:right; width:80px"> 
  21.                 專業名稱  
  22.             </td> 
  23.             <td> 
  24.                 <select id="professional" style="width: 150px"> 
  25.                 </select> 
  26.             </td> 
  27.         </tr> 
  28.     </table> 
  29. </div> 

咱們再編寫其對應的控制器以下ajax

  
  
  
  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.  
  7. namespace EducationManage.Areas.Util.Controllers  
  8. {  
  9.     using Utility.Json;  
  10.     using EducationManage.Areas.Util.Models;  
  11.     public class SelectGroupController : Controller  
  12.     {  
  13.         //  
  14.         // GET: /Util/SelectGroup/  
  15.         SelectgroupEntities selectgroupEntities = new SelectgroupEntities();  
  16.  
  17.         /// <summary>  
  18.         /// 招生批次  
  19.         /// 李磊 2010-10-29  
  20.         /// </summary>  
  21.         /// <returns></returns>  
  22.         public JsonResult GetAdmissionBatch()  
  23.         {  
  24.             List<SG_Admission_Batchs> admissionBatchsList = selectgroupEntities.admission_batchs.ToList();  
  25.             return Json(admissionBatchsList, JsonRequestBehavior.AllowGet);  
  26.         }  
  27.  
  28.         /// <summary>  
  29.         /// 學歷層次  
  30.         /// 李磊 2010-10-29  
  31.         /// </summary>  
  32.         /// <returns></returns>  
  33.         public JsonResult GetEducationLevel()  
  34.         {  
  35.             List<SG_Education_Levels> educationLevelList = selectgroupEntities.education_levels.ToList();  
  36.             return Json(educationLevelList, JsonRequestBehavior.AllowGet);  
  37.         }  
  38.  
  39.         /// <summary>  
  40.         /// 專業  
  41.         /// 李磊 2010-10-29  
  42.         /// </summary>  
  43.         /// <returns></returns>  
  44.         public JsonResult GetProfessional()  
  45.         {  
  46.             List<SG_Professional> professionalList = selectgroupEntities.professional.ToList();  
  47.             return Json(professionalList, JsonRequestBehavior.AllowGet);  
  48.         }  
  49.  
  50.         /// <summary>  
  51.         /// 學籍批次  
  52.         /// 李磊 2010-10-29  
  53.         /// </summary>  
  54.         /// <returns></returns>  
  55.         public JsonResult GetRollBatch()  
  56.         {  
  57.             List<SG_Roll_Batchs> rollBatchList = selectgroupEntities.roll_batchs.ToList();  
  58.             return Json(rollBatchList, JsonRequestBehavior.AllowGet);  
  59.         }  
  60.  
  61.         /// <summary>  
  62.         /// 專業學歷層次聯動  
  63.         /// 李磊 2010-10-29  
  64.         /// </summary>  
  65.         /// <param name="id"></param>  
  66.         /// <returns></returns>  
  67.         public JsonResult GetProfessionalByEducationLevel(string id)  
  68.         {  
  69.             try 
  70.             {  
  71.                 List<string> professionalIdList = selectgroupEntities.professional_educationlevel.Where(pe => pe.education_id == id).Select(pe => pe.prefessional_code).ToList();  
  72.                 List<SG_Professional> professionalList = selectgroupEntities.professional.Where(p => professionalIdList.Contains(p.prefessional_code)).ToList();  
  73.                 return Json(professionalList, JsonRequestBehavior.AllowGet);  
  74.             }  
  75.             catch 
  76.             {  
  77.                 return null;  
  78.             }  
  79.         }  
  80.     }  
  81. }  

好的,咱們接着編寫js.app

 首先在js的頂層引入 ///<reference path = "../../../Scripts/jQuery-1.4.1.js"/>這樣編寫js代碼就有智能提示,以下ide

  
  
  
  
  1. $(document).ready(function () {  
  2.       
  3.     $.ajaxSetup({  
  4.         cache: false 
  5.     });  
  6.     $.getJSON("/SelectGroup/GetAdmissionBatch/"function (data) {  
  7.         $("#admissionBatch").append("<option value=''>請選擇</option>");  
  8.         $.each(data, function (i, item) {  
  9.             $("<option></option>")  
  10.             .val(item["admission_batch_id"])  
  11.             .text(item["name"])  
  12.             .appendTo($("#admissionBatch"));  
  13.         });  
  14.  
  15.     });  
  16.  
  17.     $.getJSON("/SelectGroup/GetEducationLevel/"function (data) {  
  18.         $("#edcuationLevel").append("<option value=''>請選擇</option>");  
  19.         $.each(data, function (i, item) {  
  20.             $("<option></option>")  
  21.             .val(item["education_id"])  
  22.             .text(item["name"])  
  23.             .appendTo($("#edcuationLevel"));  
  24.         });  
  25.  
  26.     });  
  27.  
  28.     $.getJSON("/SelectGroup/GetProfessional/"function (data) {  
  29.         $("#professional").append("<option value=''>請選擇</option>");  
  30.         $.each(data, function (i, item) {  
  31.             $("<option></option>")  
  32.             .val(item["prefessional_code"])  
  33.             .text(item["name"])  
  34.             .appendTo($("#professional"));  
  35.         });  
  36.  
  37.     });  
  38.  
  39.     $("#edcuationLevel").change(function () {  
  40.         $("#professional").empty();  
  41.         $("#professional").append("<option value='0'>請選擇</option>");  
  42.         $.getJSON("/SelectGroup/GetProfessionalByEducationLevel/" + $("#edcuationLevel").val(), function (data) {  
  43.             $.each(data, function (i, item) {  
  44.                 $("<option></option>")  
  45.             .val(item["prefessional_code"])  
  46.             .text(item["name"])  
  47.             .appendTo($("#professional"));  
  48.             });  
  49.  
  50.         });  
  51.     });  
  52.  
  53. }) 

ok,好了,咱們看看在頁面怎麼引用spa

  
  
  
  
  1. <%Html.RenderPartial("~/Areas/Util/Views/Shared/SelectGroup.ascx"); %> 

只要你將這段代碼放在頁面上便可。Html.RenderPartial有不少重載,你也能夠給用戶控件傳遞一個須要綁定的對象。說到這裏談談Html.RenderPartial和Html.Partial的區別。Html.RenderPartial是直接輸出至當前HttpContext,而Html.Partial是將視圖內容直接生成一個字符串並返回。因此在引用的時候不同分別爲<% Html.RenderPartial("~/Areas/Util/Views/Shared/SelectGroup.ascx"); %>和<%= Html.Partial("~/Areas/Util/Views/Shared/SelectGroup.ascx"); %>。說到這兩個難免要說Html.RenderAction,它是經過Controller中的Action來調用用戶控件。上面的代碼你們能夠根據js和控制器代碼看到,每一個下拉列表的綁定都有本身的控制器返回數據,在頁面加載完成的時候去調用本身的控制器獲取下拉列表數據。若是咱們用Html.RenderAction就沒有那麼麻煩了,看看控制器代碼3d

  
  
  
  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.  
  7. namespace EducationManage.Areas.Util.Controllers  
  8. {  
  9.     using Utility.Json;  
  10.     using EducationManage.Areas.Util.Models;  
  11.     public class SectionGroupController : Controller  
  12.     {  
  13.         //  
  14.         // GET: /Util/SectionGroup/  
  15.         SelectgroupEntities selectgroupEntities = new SelectgroupEntities();  
  16.         public ActionResult Index()  
  17.         {  
  18.             List<SG_Admission_Batchs> admissionBatchList = selectgroupEntities.admission_batchs.ToList();  
  19.             SelectList admissionBatch = new SelectList(admissionBatchList, "admission_batch_id""name""");  
  20.             ViewData.Add("admissionBatch", admissionBatch);  
  21.  
  22.             List<SG_Education_Levels> educationLevelList = selectgroupEntities.education_levels.ToList();  
  23.             SelectList educationLevel = new SelectList(educationLevelList, "education_id""name""");  
  24.             ViewData.Add("educationLevel", educationLevel);  
  25.  
  26.             List<SG_Professional> professionalList = selectgroupEntities.professional.ToList();  
  27.             SelectList professional = new SelectList(professionalList, "prefessional_code""name""");  
  28.             ViewData.Add("professional", professional);  
  29.             return PartialView("~/Areas/Util/Views/Shared/SectionGroup.ascx");  
  30.         }  
  31.  
  32.     }  
  33. }  

再看看前臺code

  
  
  
  
  1. <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> 
  2. <script language="javascript" type="text/javascript" src="<%=Url.Content("~/Areas/Util/Scripts/SelectGroup.js") %>/> 
  3. <div> 
  4.     <table> 
  5.         <tr> 
  6.             <td style="text-align: right"> 
  7.                 招生批次 
  8.             </td> 
  9.             <td> 
  10.                 <%:Html.DropDownList("admissionBatch", ViewData["admissionBatch"] as SelectList, "請選擇", new { id = "admissionBatch"style = "width: 150px" })%> 
  11.             </td> 
  12.             <td style="text-align: right; width: 80px"> 
  13.                 學歷層次 
  14.             </td> 
  15.             <td> 
  16.                 <%:Html.DropDownList("edcuationLevel", ViewData["educationLevel"] as SelectList, "請選擇", new { id = "edcuationLevel"style = "width: 150px" })%> 
  17.             </td> 
  18.             <td style="text-align: right; width: 80px"> 
  19.                 專業名稱 
  20.             </td> 
  21.             <td> 
  22.                 <%:Html.DropDownList("professional", ViewData["professional"] as SelectList, "請選擇", new { id = "professional"style = "width: 150px" })%> 
  23.             </td> 
  24.         </tr> 
  25.     </table> 
  26. </div> 

在這裏咱們一次性就獲取到了全部下拉列表要綁定的數據。咱們只須要在前臺這樣應用便可<%Html.RenderAction("Index", "SectionGroup");%>。好了,在MVC2中使用用戶控件就是這麼簡單。xml

相關文章
相關標籤/搜索