在ASP.NET MVC中如何讓ASCX使用者控制項具有Action / Controller

來源:互聯網
上載者:User

一般在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/

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.