Domanda

Qualcuno conosce la sintassi per la creazione di un metodo HtmlHelperextension personalizzato che si comporta come ..

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

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

<% } %>

Sto pensando a qualcosa sulla falsariga di ....

Qualche idea?

Saluti,

ETFairfax

È stato utile?

Soluzione

Devi creare una classe che implementa l'interfaccia IDisposable e restituirla dal tuo 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...
    }
}

Altri suggerimenti

Dovresti avere un metodo simile a questo:

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

    return new EndTableWriter();
}

Dove EndTableWriter è qualcosa del genere:

private class EndTableWriter : IDisposable
{
    public void Dispose()
    {
        // write the end of the table here
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top