문제

누구든지 사용자 정의 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