简体   繁体   English

ASP.NET MVC:何时使用自定义HTML帮助程序方法vs Html.RenderAction?

[英]ASP.NET MVC: When to use custom HTML helper methods vs Html.RenderAction?

It's a little unclear for me on when to use a custom helper method and when to use RenderAction and also when to simply use ViewData instead. 关于何时使用自定义辅助方法以及何时使用RenderAction以及何时简单地使用ViewData,我有点不清楚。 Some of their functions overlap slightly. 它们的一些功能略有重叠。

For example, if I were to create a Category navigation bar, would I create a new helper method and place that in some partial view? 例如,如果我要创建一个类别导航栏,我是否会创建一个新的辅助方法并将其放在某个局部视图中? I had initially though of doing this, but I read on some blog to use RenderAction instead. 我最初虽然这样做,但我在一些博客上读到使用RenderAction代替。 I've just been thinking back and forth and could use some help not just with this example, but in general. 我一直在思考,并且可以使用一些帮助而不仅仅是这个例子,但总的来说。

Assume the list of categories is coming from some data source. 假设类别列表来自某些数据源。

The general guidelines that I follow are: 我遵循的一般准则是:

HtmlHelper methods: HtmlHelper方法:

  1. Used to standardize markup. 用于标准化标记。 I use helpers to make sure my form fields, input buttons and image tags use consistent markup. 我使用帮助程序来确保我的表单字段,输入按钮和图像标记使用一致的标记。
  2. Used when the resulting markup is minimal. 在生成的标记最小时使用。 Small bits of text, form field markup, etc. I don't use helpers to render full domain objects. 少量文本,表单字段标记等。我不使用帮助器来呈现完整的域对象。
  3. Operate on a small number of discrete arguments. 操作少量离散参数。 If I need to iterate over a collection and display something, that's a partial. 如果我需要迭代一个集合并显示一些东西,那就是部分的。 If I need to take a large amount of input, that's a partial too. 如果我需要大量的输入,那也是一个部分。
  4. Do not contain any business logic, just presentation logic. 不包含任何业务逻辑,只包含表示逻辑。 The arguments are usually objects in the solution domain, not the business/problem domain. 参数通常是解决方案域中的对象,而不是业务/问题域。
  5. Are usually very general in scope and apply to large portions of the application. 通常非常通用,适用于大部分应用程序。

Render partial: 渲染部分:

  1. Used when I want to decompose a large view into smaller pieces. 当我想将大视图分解成更小的片段时使用。 The model should be a subset of the model of the "main" view. 该模型应该是“主”视图模型的子集。
  2. Partial views are often used only by certain controllers or areas. 部分视图通常仅由某些控制器或区域使用。

Render action: 渲染动作:

  1. Used when I want to create small chunks of functionality that can be composed in various ways. 当我想创建可以以各种方式组合的小块功能时使用。
  2. Most often used to generate content that applies to many controllers or areas, such as navigation controls. 最常用于生成适用于许多控制器或区域的内容,例如导航控件。

ViewData: ViewData的:

I'll use ViewData to track global data that applies to all views, such as the current user. 我将使用ViewData来跟踪适用于所有视图的全局数据,例如当前用户。 If I need a consistent way of displaying this data I usually create a partial for it and then do RenderPartial() in a master page. 如果我需要一种显示此数据的一致方法,我通常会为它创建一个部分,然后在母版页中执行RenderPartial()。

Firstly, this is probably clear, but let's say it: The category business logic (eg fetching data from a data source) should not be in Html-helper or in a user control: it should be done in a controller. 首先,这可能很清楚,但是我们可以这样说:类别业务逻辑(例如,从数据源获取数据)不应该在Html帮助程序中或在用户控件中:它应该在控制器中完成。

The difference between 1) RenderPartial / HtmlHelper vs. 2) RenderAction is in which controller this business logic is: 1)RenderPartial / HtmlHelper与2)RenderAction之间的区别在于该业务逻辑在哪个控制器中:

  1. in the one controller action that does the whole page or 在一个控制器动作中执行整个页面或
  2. in a separate controller action specific to the partial view. 在特定于部分视图的单独控制器操作中。

If you use your category data in pretty much every page I do not see it wrong to fetch it for each page on page-controller action level and pass it in the view data. 如果您在几乎每个页面中使用类别数据,我认为在页面控制器操作级别上为每个页面获取它并不是错误的,并将其传递给视图数据。 Of course you would use some mechanism (custom model base class, extend controller, ...) so that you don't have the same category-fetching function call in each action (assuming you have lots). 当然你会使用一些机制(自定义模型基类,扩展控制器......),这样你就不会在每个动作中都有相同的类别获取函数调用(假设你有很多)。

If some page views choose to show the categories, some not and some perhaps have another category control with different business logic, then RenderAction is definitely better. 如果某些页面视图选择显示类别,有些不是,有些可能有另一个具有不同业务逻辑的类别控件,那么RenderAction肯定更好。 Even in the above case, RenderAction is good: it separates the category fetching from other data in your controller actions. 即使在上述情况下,RenderAction也很好:它将类别提取与控制器操作中的其他数据分开。

Then whether to use RenderPartial or HtmlHelper... To me HtmlHelpers should be more generic and not specific to particular view or model, but this, I suppose, is more matter of taste than clear rule from MVC perspective: both should be just View-logic. 然后是否使用RenderPartial或HtmlHelper ......对我而言,HtmlHelpers应该更通用,而不是特定于特定视图或模型,但我认为,这比MVC视角中的明确规则更具品味:两者都应该只是View-逻辑。

I would choose html helper methods when the scenario meets these criteria: 当场景满足以下条件时,我会选择html辅助方法:

  1. The arguments are not considered model data 参数不被视为模型数据
  2. It doesn't have to generate an excessive amount of markup 它不必生成过多的标记
  3. The html can be generated with just the arguments given to it 可以使用给定的参数生成html

If you have an html helper method using model data or it has a lot of dependencies, its probably better as a RenderPartial or RenderAction. 如果你有一个使用模型数据的html辅助方法,或者它有很多依赖项,那么它可能更适合作为RenderPartial或RenderAction。

I'm new also to using RenderAction 我也是使用RenderAction的新手

but when I need to load data for a specific piece of display I now go with RenderAction 但是当我需要为特定的显示器加载数据时,我现在使用RenderAction

Ideal for loading Tag cloud, which are displayed on every page but the data is not specific to a page. 非常适合加载标签云,标签云显示在每个页面上,但数据并非特定于页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM