简体   繁体   English

自定义HTML帮助程序类是否违反ASP.NET MVC模型?

[英]Do custom HTML helper classes violate the ASP.NET MVC model?

On a related post I mentioned that I have found custom HTML helpers to be just that, helpful, when developing. 在我提到的一篇相关文章中,我发现自定义HTML帮助程序在开发时非常有用。 For instance, when I need paging for a "grid" I have a custom helper that I can call Html.Pager() . 例如,当我需要分页“网格”时,我有一个自定义助手,我可以调用Html.Pager()

Some have made a point that HTML helpers are a violation of the MVC model. 有些人指出HTML助手违反了MVC模型。 Personally, I don't see it being any different than the existing helpers, such as Html.Textbox() or Html.ActionLink() . 就个人而言,我认为它与现有助手没有任何不同,例如Html.Textbox()Html.ActionLink()

I'm still trying to learn more about MVC, so all perspectives are appreciated. 我还在尝试更多地了解MVC,所以所有观点都值得赞赏。

Notice that the existing helpers are all written as extension methods of the HtmlHelper class. 请注意,现有的帮助程序都是作为HtmlHelper类的扩展方法编写的。 We explicitly took that approach so that others can write their own helper methods as extension methods of HtmlHelper. 我们明确地采用了这种方法,以便其他人可以编写自己的辅助方法作为HtmlHelper的扩展方法。

So in general, this is not a violation of the MVC model. 所以一般来说,这不违反MVC模型。 I guess it really depends on what you are doing in your helper. 我想这真的取决于你在助手中做了什么。 Helpers should simply render html based on arguments passed into them. 助手应该根据传递给它们的参数简单地渲染html。 They shouldn't do any data access, etc... 他们不应该做任何数据访问等...

They merely encapsulate code for rendering common pieces of markup. 它们仅封装用于呈现常见标记的代码。 If you're doing that, then you're not in violation of the ASP.NET MVC model. 如果你这样做,那么你就不会违反ASP.NET MVC模型。

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

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