ASP.NET MVC 3 Razor Templates VS RenderPartial

后端 未结 2 1975
遇见更好的自我
遇见更好的自我 2021-01-30 18:09

I just read this blog post on Razor Templating in ASP.NET MVC 3.

Put simply, i just dont get it!

That is, i don\'t see why we need this

2条回答
  •  时光取名叫无心
    2021-01-30 18:28

    Well, you should ask the author of that post about his motivation for presenting this technique.

    It certainly illustrates what is possible in Razor. Whether you should use it is a different matter. I personally think that there are alternative techniques that are less complicated (I agree with your points about storing a Func inside of the request context).

    • There's @RenderPartial which you already mentioned.
    • You can also use the @helper syntax (either as a local helper or a global helper)
    • You can write an html helper (and use TagBuilder to assemble the output)
    • You can write a child action
    • You can write a templated helper

    Now that I look at the above list I think MVC might provide too much choice :)

    Update To better illustrate how inline templates can be useful I wrote a blog post about using them to call sections with default code: Optional Razor Sections with Default Content.

    You can use it to write something like this:

    @this.RenderSection("OptionalSection", @
    Default Content
    )

提交回复
热议问题