用户控件在ASP中的等效。净MVC 4

时间:2022-12-12 16:48:32

I'm working on a new ASP.NET MVC 4 app. I'm really trying to do things the "MVC" way. In particular, I'm interested in using the Razor view engine. Please note that I come from a web forms background, so my questions may seem awkward.

我正在开发一个新的ASP。NET MVC 4 app,我真的是在尝试用MVC的方式做事。特别是,我对使用Razor视图引擎很感兴趣。请注意,我来自web forms的背景,所以我的问题可能会显得有些尴尬。

I'm trying to learn if there is something equivalent to user controls in MVC? If so, what are they called? If not, what is the recommended way to creating a reusable component in MVC?

我想知道在MVC中是否存在与用户控件等价的东西?如果是,他们叫什么?如果不是,那么在MVC中创建可重用组件的推荐方式是什么?

2 个解决方案

#1


29  

You're looking for @Html.Action and @Html.Partial.

你正在寻找@Html。行动和@Html.Partial。

They allow you to either render a partial view or a full blown controller action in a view. This is a common pattern of reuse in MVC land.

它们允许您在视图中呈现部分视图或完整的控制器操作。这是MVC领域中常见的重用模式。

Occasionally you would want to make displayFor or editorFor templates, if a controller action is too heavy. The rule of thumb is if you need to do it multiple times on the page and it needs to be posted back in a form, think about doing it in a template.

如果控制器动作太大,您偶尔会希望为模板创建displayFor或editorFor。经验法则是,如果你需要在页面上做很多次,并且需要以表单的形式发布,考虑在模板中做。

Controls in asp.net cover a rather large swath which MVC granularizes a bit more.

asp.net中的控件覆盖了相当大的范围,MVC将其细化。

#2


11  

To creating a reusable HTML component in MVC you can create a partial view in the Views/Shared folder and use @HTML.Partial("_PartialViewName") to include it in any other view or partial view. You can find out more about partial views in this article.

要在MVC中创建可重用的HTML组件,您可以在Views/Shared文件夹中创建部分视图,并使用@HTML.Partial(“_PartialViewName”)将其包含在任何其他视图或部分视图中。您可以在本文中了解更多关于部分视图的内容。

#1


29  

You're looking for @Html.Action and @Html.Partial.

你正在寻找@Html。行动和@Html.Partial。

They allow you to either render a partial view or a full blown controller action in a view. This is a common pattern of reuse in MVC land.

它们允许您在视图中呈现部分视图或完整的控制器操作。这是MVC领域中常见的重用模式。

Occasionally you would want to make displayFor or editorFor templates, if a controller action is too heavy. The rule of thumb is if you need to do it multiple times on the page and it needs to be posted back in a form, think about doing it in a template.

如果控制器动作太大,您偶尔会希望为模板创建displayFor或editorFor。经验法则是,如果你需要在页面上做很多次,并且需要以表单的形式发布,考虑在模板中做。

Controls in asp.net cover a rather large swath which MVC granularizes a bit more.

asp.net中的控件覆盖了相当大的范围,MVC将其细化。

#2


11  

To creating a reusable HTML component in MVC you can create a partial view in the Views/Shared folder and use @HTML.Partial("_PartialViewName") to include it in any other view or partial view. You can find out more about partial views in this article.

要在MVC中创建可重用的HTML组件,您可以在Views/Shared文件夹中创建部分视图,并使用@HTML.Partial(“_PartialViewName”)将其包含在任何其他视图或部分视图中。您可以在本文中了解更多关于部分视图的内容。