MVC 3 Razor, Helpers with custom markup/section

前端 未结 4 630
囚心锁ツ
囚心锁ツ 2021-02-05 21:16

I\'m not even sure if this is possible, but I thought I would check to see if there is any way to make this easier.

First, I have some repeated markup in my site that lo

4条回答
  •  迷失自我
    2021-02-05 21:42

    There's also the other way, without disposable trick, which also requires a little less work, great for little helpers.

    @helper MyHelper(string title, Func markup) {
        

    Title

    @markup.DynamicInvoke(this.ViewContext)

    }

    Usage of this helper looks like this:

    @MyHelper("This is my Title", 
        @

    Here is my custom markup

    )

    Or with multiple lines:

    @MyHelper("This is my Title", 
        @
            

    More than one line

    Of markup

    )

    Telerik MVC controls used this trick for example to let you add your javascript code at the document load.

    Here's also a nice example. There's also some information here.

提交回复
热议问题