Asp.Net MVCでテンプレートコントロールを作成するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/1822272

  •  10-07-2019
  •  | 
  •  

質問

Asp.Net MVCでテンプレートコントロールを作成しようとしています。テンプレート化されたコントロールとは、次のような入力としてマークアップを受け入れるコントロールを意味します。

<% Html.PanelWithHeader()
    .HeaderTitle("My Header")
    .Content(() =>
    { %>
        <!-- ul used for no particular reason -->
        <ul>
          <li>A sample</li>
          <li>A second item</li>
        </ul>
    <% }).Render(); %>

注:はい、これは TelerikがMVCコントロールを作成する方法に非常に似ています、私は構文が好きです。

ここに私のPanelWithHeaderコードがあります:

// Extend the HtmlHelper
public static PanelWithHeaderControl PanelWithHeader(this HtmlHelper helper)
{
    return new PanelWithHeaderControl();
}

public class PanelWithHeaderControl
{
    private string headerTitle;

    private Action getContentTemplateHandler;

    public PanelWithHeaderControl HeaderTitle(string headerTitle)
    {
        this.headerTitle = headerTitle;

        return this;
    }

    public PanelWithHeaderControl Content(Action getContentTemplateHandler)
    {
        this.getContentTemplateHandler = getContentTemplateHandler;

        return this;
    }

    public void Render()
    {
        // display headerTitle as <div class="header">headerTitle</div>

        getContentTemplateHandler();
    }
}

これはulを表示しますが、Renderメソッド内でカスタムコードを表示する方法がわかりません。

HtmlHelperを使用しようとしましたが成功しませんでした。また、<%=Html.PanelWithHeader()...構文を使用できるようにToStringメソッドをオーバーライドしようとしましたが、構文エラーが発生し続けました。

これを行うにはどうすればよいですか

役に立ちましたか?

解決 2

Telerik MVC拡張機能はオープンソースであり、CodePlexで利用可能です。ソースコードをざっと見てください。

HtmlHelperインスタンスのViewContextからHtmlTextWriterを作成します。彼らがそれに書き込むと、ページに書き込みます。

コードは次のようになります。

// Extend the HtmlHelper
public static PanelWithHeaderControl PanelWithHeader(this HtmlHelper helper)
{
    HtmlTextWriter writer = helper.ViewContext.HttpContext.Request.Browser.CreateHtmlTextWriter(helper.ViewContext.HttpContext.Response.Output);

    return new PanelWithHeaderControl(writer);
}

public class PanelWithHeaderControl
{
    private HtmlTextWriter writer;

    private string headerTitle;

    private Action getContentTemplateHandler;

    public PanelWithHeaderControl(HtmlTextWriter writer)
    {
        this.writer = writer;
    }

    public PanelWithHeaderControl HeaderTitle(string headerTitle)
    {
        this.headerTitle = headerTitle;

        return this;
    }

    public PanelWithHeaderControl Content(Action getContentTemplateHandler)
    {
        this.getContentTemplateHandler = getContentTemplateHandler;

        return this;
    }

    public void Render()
    {
        writer.Write("<div class=\"panel-with-header\"><div class=\"header\">" + headerTitle + "</div><div class=\"content-template\">");

        getContentTemplateHandler();

        writer.Write("</div></div>");
    }
}

*コードは混乱している

他のヒント

public void Render()
{
    Response.Write(getContentTemplateHandler());
}

Html.BeginPanel() / Html.EndPanel()を使用してフォームを作成する方法と同様に、Html.BeginForm() / Html.EndForm()のような操作を実行できます。これにより、パラメーターとして渡す必要なく、含まれているコンテンツをラップできます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top