本文介绍了ASP.NET MVC 3剃须刀模板VS的RenderPartial的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚才读this在博客文章 剃刀模板在ASP.NET MVC 3。

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

简而言之,的我只是不明白!

这是,我不明白为什么我们需要这个(相当)复杂code实现,有什么可以用 @RenderPartial ?

That is, i don't see why we need this (fairly) complicated code to achieve what can be done IMO easier (and neater) with @RenderPartial ?

下面就是我不喜欢的:


  1. 模板存储为 Func键< T,HelperResult> 委托

  2. 该模板代表在控制器ViewData的坚持(如HttpContext.Current.Items)

我从博客看唯一的好处是不需要模板一个单独的文件,这意味着你不需要重新编译等。

The only "benefit" i read from that blog is that a seperate file isn't required for templating, meaning you don't need to re-compile etc.

但我不认为这是一个有效的论据。额外的文件都很好,只要解决方案的组织不会受到损害。

But i don't see that as a valid argument. Extra files are fine as long as the solution organization isn't compromised.

我preFER @RenderPartial ,因为我可以保持我的独立的标记从主视图,我可以使这个内联(渲染时间),并用jQuery的(如AJAX事件)。

I prefer using @RenderPartial, as i can keep my markup seperate from the master view, and i can render this both inline (render time) and with jQuery (e.g AJAX event).

也许我失去了一些东西,但任何人都可以给一些原因,我们应该选择剃刀模板过的RenderPartial来创建可重用的内容?

Maybe i'm missing something here, but can anyone give some reasons why we should choose Razor Templating over RenderPartial to create re-usable content?

推荐答案

好了,你应该问这个职位对他的动机presenting这项技术的作者。

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).


  • @RenderPartial 您已经提到。

  • 您也可以使用 @helper 语法(无论是作为本地助手或全局助手)

  • 您可以编写一个HTML帮手(并使用 TagBuilder 来装配输出)

  • 您可以编写一个子操作

  • 您可以写一个模板帮手

  • 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

现在,我看上面的列表中,我认为MVC可能提供太多的选择:)

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

更新为了更好地说明如何模板内嵌可我写的关于利用它们来调用默认code部分的博客文章有用:Optional剃刀章节用默认内容。

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", @<div>Default Content</div>) 

这篇关于ASP.NET MVC 3剃须刀模板VS的RenderPartial的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 23:51