在MVC之前,當webforms是我們唯一的東西時,我曾經爲我的控件定義了一個容器(可以調用一個框),並使用這個web控件(標題,正文和其他一些屬性)。在asp.net中的控制模板mvc
所以,如果我想改變我所有的視覺控制的外觀,我只需要在我的Box控件進行一個更改。
這裏是我以前做的一個簡單的代碼片段:在
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Box.ascx.cs" Inherits="WebApp.Controls.Box" %>
<span class="boxtitle">
<asp:Literal ID="ltrTitle" runat="server"></asp:Literal>
</span>
<div class="boxContent" id="dvBox">
<asp:PlaceHolder runat="server" ID="BoxBodyContainer"/>
</div>
和代碼隱藏:
public Control BoxBody { get; set; }
public string Title
{
get { return this.ltrTitle.Text; }
set { this.ltrTitle.Text = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
this.BoxBodyContainer.Controls.Add(this.BoxBody);
}
,所以我可以在我的頁面如下其他控件一起使用:
<MyControls:Box ID="Box1" Title="Foo" runat="server">
<boxbody>
<MyControls:CustomControl ID="Bar" runat="server" >
</MyControls:CustomControl>
</boxbody>
</MyControls:Box>
爲了有東西都在MVC 3,我想我應該定義一個Box
視圖和我需要的盒子,請使用Html.Partial
或Html.Action
並將所有數據(標題,boxbody等)傳遞到此視圖,並在Box視圖中使用@Html.Raw
。
但我想知道是否有更好,更標準的方法來實現這一點。 在MVC 3(Razor)中實現這一點的正確方法是什麼?
感謝。這可能是最好的方法。 – Kamyar 2012-04-10 05:57:59