Pregunta

¿Alguien sabe la sintaxis para crear un método HtmlHelperextension personalizado que se comporte como ...

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

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

<% } %>

Estoy pensando en algo parecido a ...

¿Alguna idea?

Saludos,

ETFairfax

¿Fue útil?

Solución

Debe crear una clase que implemente la interfaz IDisposable y devolverla desde su 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...
    }
}

Otros consejos

Necesitarías tener un método como este:

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

    return new EndTableWriter();
}

Donde EndTableWriter es algo como esto:

private class EndTableWriter : IDisposable
{
    public void Dispose()
    {
        // write the end of the table here
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top