Pergunta

Alguém sabe a sintaxe para a criação de um método HtmlHelperextension costume que se comporta como ..

<% using (Html.BeginForm()) {%>

<p>Loads of html stuff here </p>

<% } %>

Estou pensando em algo ao longo das linhas de ....

Todas as idéias?

Cheers,

ETFairfax

Foi útil?

Solução

Você precisa criar uma classe que implementa IDisposable interface e retorno que do seu HtmlHelper.

public static class HtmlHelperTableExtensions {
    private class TableRenderer : IDisposable {
        HtmlHelper html;
        public TableRenderer(HtmlHelper html) {
           this.html = html;
        }
        public void Dispose() {
           HtmlHelperTableExtensions.EndTable(html);
        }
    }
    public static IDisposable BeginTable(this HtmlHelper html) {
        // print begin table here...
        return new TableRenderer(html);
    }
    public static void EndTable(this HtmlHelper html) {
        // print end table here...
    }
}

Outras dicas

Você precisa ter um método algo como isto:

public static IDisposable BeginTable(this HtmlHelper html, ...)
{
    // write the start of the table here

    return new EndTableWriter();
}

Quando o EndTableWriter é algo como isto:

private class EndTableWriter : IDisposable
{
    public void Dispose()
    {
        // write the end of the table here
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top