每天学点MVC 【用户自定义控件】

Asp.Net MVC2中 说到用户自定义控件,就一肚子火气,折腾过我好多回,这回总算看了点所以然

 什么都不说了,code附上

 1 情况一:通过controller创建的用户自定义控件

 2 //Controllers :TestAscx
 4 
 5  public ActionResult TestAscx()
 6         {
 7             Ascx ascx = new Ascx();
 8             return PartialView(ascx);
 9         }
10 //View :TestAscx.ascx
11 
12 <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Ascx>" %>
13 在里面可以填写你想要写的东东
14 
15 //调用控件
16 View: index.aspx
17 
18 <% Html.RenderAction("TestAscx"); %>

 情况二:调用ASP.NET 模式的用户自定义控件,eg:footer.ascx

Controller中不需要任何东东,只需要在需要调用的地方

<% Html.RenderPartial("footer"); %>
原文地址:https://www.cnblogs.com/bingzisky/p/2320496.html