Mvc 3 Razor:如何使用节(section)来呈现局部视图(partial view)?

10
我在部分视图中定义了一个区域,我想要从视图中指定该区域的内容。但我找不到方法。在asp.net用户控件中,我们可以定义asp:placeholders,并从aspx文件中指定内容。如果有任何建议,我将非常感激。
谢谢。
[编辑] 这是asp.net用户控件,我想将其转换为razor部分视图。
用户控件:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SpryListView.ascx.cs" Inherits="SpryListView" %>
<div spry:region="<%=this.SpryDataSetName%>" id="region<%=this.ID%>" style="overflow:auto;<%=this.DivStyle%>" >
<table class="searchList" cellspacing="0" style="text-align:left" width="100%">
    <thead>
        <tr>
            <asp:PlaceHolder ID="HeaderColumns" runat="server"></asp:PlaceHolder>
        </tr>
    </thead>
</table>

用户控制代码:

public partial class SpryListView : System.Web.UI.UserControl
{
    private string spryDataSetName ;
    private string noDataMessage = "Aradığınız kriterlere uygun kayıt bulunamadı.";
    private bool callCreatePaging;
    private string divStyle;
    private ITemplate headers = null;
    private ITemplate body = null;

    [TemplateContainer(typeof(GenericContainer))]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public ITemplate HeaderTemplate
    {
        get
        {
            return headers;
        }
        set
        {
            headers = value;
        }
    }

    [TemplateContainer(typeof(GenericContainer))]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public ITemplate BodyTemplate
    {
        get
        {
            return body;
        }
        set
        {
            body = value;
        }
    }

    public string DivStyle
    {
        get { return divStyle; }
        set { divStyle= value; }
    }

    public string NoDataMessage
    {
        get { return noDataMessage; }
        set { noDataMessage = value; }
    }

    public string SpryDataSetName
    {
        get { return spryDataSetName; }
        set { spryDataSetName = value; }
    }

    public bool CallCreatePaging
    {
        get { return callCreatePaging; }
        set { callCreatePaging = value; }
    }

    void Page_Init()
    {
        if (headers != null)
        {
            GenericContainer container = new GenericContainer();
            headers.InstantiateIn(container);
            HeaderColumns.Controls.Add(container);

            GenericContainer container2 = new GenericContainer();
            body.InstantiateIn(container2);
            BodyColumns.Controls.Add(container2);
        }
    }

    public class GenericContainer : Control, INamingContainer
    {
        internal GenericContainer()
        {

        }

    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

aspx

<spry:listview SpryDataSetName="dsOrders" CallCreatePaging="true" runat="server" ID="orderListView">
    <HeaderTemplate>
        <th>&nbsp;</th>
        <th>SİPARİŞ TARİHİ</th>
        <th style="text-align:right">GENEL TOPLAM</th>
        <th style="text-align:right">KDV</th>
        <th style="text-align:right">NET TOPLAM</th>
    </HeaderTemplate>  
 </spry:listview>

我希望在MVC 3 Razor部分视图中做到这一点。
2个回答

13

模板化Razor委托似乎是你需要的内容。它们基本上允许你的辅助函数将模板(你的委托)作为来自视图的参数进行传递。这样,调用者(你的视图)控制信息呈现的方式,而不是辅助函数,从而给你更多的灵活性。


1
链接已经失效。有效链接:http://haacked.com/archive/2011/02/26/templated-razor-delegates.aspx/ - Andrew

4
你应该将 Func<object, HelperResult> 作为局部视图的参数传递。
在父视图中,你可以将像 @<div>...</div> 这样的HTML代码作为参数传递。
在局部视图中,你可以使用任何参数来调用委托以呈现HTML。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接