在ASP.NET MVC中如何让ASCX用户控件具有Action / Controller

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

转载于:https://www.cnblogs.com/xmily/archive/2013/05/11/3072343.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值