سؤال

لا أحد يعرف بناء الجملة من أجل خلق طريقة HtmlHelperextension العرف الذي يتصرف مثل ..

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

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

<% } %>

وأنا أفكر في شيء على غرار ....

وأي أفكار؟

وابتهاج،

وETFairfax

هل كانت مفيدة؟

المحلول

وتحتاج إلى إنشاء فئة التي تطبق الواجهة IDisposable والعودة التي من 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...
    }
}

نصائح أخرى

وكنت بحاجة إلى طريقة شيئا من هذا القبيل:

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

    return new EndTableWriter();
}

وأين EndTableWriter شيء من هذا القبيل:

private class EndTableWriter : IDisposable
{
    public void Dispose()
    {
        // write the end of the table here
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top