2009-10-30 13 views
6

Czy ktoś zna składnię utworzyć niestandardową metodę HtmlHelperextension który zachowuje się jak ..Html.BeginForm() typ rozszerzenia

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

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

<% } %> 

myślę o czymś wzdłuż linii ....

Wszelkie pomysły?

Cheers,

ETFairfax

Odpowiedz

8

You trzeba utworzyć klasę, która implementuje interfejs IDisposable i ret urn to od twojego 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... 
    } 
} 
+0

fajny. Dzięki Mehrdad – ETFairfax

+3

Aby wyjaśnić, że czytasz to, musisz zadzwonić do HttpContext.Current.Response.Write (""); w miejscach, które Mehrdadhas napisał // print begin table here i // print end table here. – ETFairfax

1

trzeba by mieć metoda A coś takiego:

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

    return new EndTableWriter(); 
} 

Jeżeli EndTableWriter jest mniej więcej tak:

private class EndTableWriter : IDisposable 
{ 
    public void Dispose() 
    { 
     // write the end of the table here 
    } 
} 
Powiązane problemy