一般在MVC页面中我们使用ASCX用户控件我们这样写:
<%=Html.Partial("AdminViewLeftMenu") %>
或者这样写:
<%Html.RenderPartial("~/Areas/Util/Views/Shared/SelectGroup.ascx"); %>
上面两种引用方法的区别:
Html.RenderPartial是直接输出至当前HttpContext,而Html.Partial是将视图内容直接生成一个字符串并返回。所以在引用的时候不一样分别为<% Html.RenderPartial("~/Areas/Util/Views/Shared/SelectGroup.ascx"); %>和<%= Html.Partial("~/Areas/Util/Views/Shared/SelectGroup.ascx"); %>。
当需要给ASCX用户控件添加action的时候我们就要像下在这样引用 :
<%Html.RenderAction("Index", "SectionGroup");%>
Action如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace EducationManage.Areas.Util.Controllers { using Utility.Json; using EducationManage.Areas.Util.Models; public class SectionGroupController : Controller { // // GET: /Util/SectionGroup/ SelectgroupEntities selectgroupEntities = new SelectgroupEntities(); public ActionResult Index() { List<SG_Admission_Batchs> admissionBatchList = selectgroupEntities.admission_batchs.ToList(); SelectList admissionBatch = new SelectList(admissionBatchList, "admission_batch_id", "name", ""); ViewData.Add("admissionBatch", admissionBatch); List<SG_Education_Levels> educationLevelList = selectgroupEntities.education_levels.ToList(); SelectList educationLevel = new SelectList(educationLevelList, "education_id", "name", ""); ViewData.Add("educationLevel", educationLevel); List<SG_Professional> professionalList = selectgroupEntities.professional.ToList(); SelectList professional = new SelectList(professionalList, "prefessional_code", "name", ""); ViewData.Add("professional", professional); return PartialView("~/Areas/Util/Views/Shared/SectionGroup.ascx"); } } }
以上内容参考这篇文章:http://leelei.blog.51cto.com/856755/412702/