• 在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/

  • 相关阅读:
    (五)SpringBoot如何定义全局异常
    从 vim 一题看线头 DP 的扩展应用
    Hadoop Shell基本操作
    《需求工程》阅读笔记*part1
    Jmeter系列(16)- 详解 HTTP Request
    Jmeter系列(15)- 配置元件的入门介绍
    Jmeter系列(14)- 前置、后置处理器的入门介绍
    Jmeter系列(13)- 断言Assertions 的入门介绍
    Jmeter系列(12)- 定时器Timers 的入门介绍
    Jmeter系列(11)- 监听器Listeners 的入门介绍
  • 原文地址:https://www.cnblogs.com/xmily/p/3072343.html
Copyright © 2020-2023  润新知