如何编写 MVC3 Helper 代码
我刚刚在 MVC 中构建了我的第一个 Helper,它非常基本,只是在我使用它的地方显示一个字符串。所以它是我的 App_Code 文件夹中的一个 .cshtml 文件,我认为这就是它应该如何设置的,其中包含以下代码,
@helper DisplaySelect() {
@:This text is coming from an helper class.
}
现在我是一个有助手的向导,我如何让它做事。例如。假设我希望它查询数据库并显示一些内容,我通常会在我的控制器中完成这项工作。我如何使用助手来做到这一点,我是否创建一个助手控制器,然后将助手视为部分视图???
任何帮助将不胜感激。
干杯,
迈克。
谢谢大家,
我在 Shark 之前问过这个问题“为什么你会使用助手而不是部分视图”我得到的答案是部分视图更多的是用于显示常见的 HTML,其中作为助手可以有一堆代码在里面做各种很棒的处理工作。现在看来这不是真的,在某些方面它们几乎是同一件事,除了 Link664 的情况!
我喜欢你所说的 Link664,它很有意义,因为它很好地清理了代码。
我要做的是尝试在助手中填充下拉列表,然后在多个地方使用它,但从我今天的研究来看,这不是助手的用途。 干杯,
迈克。
I’ve just build my first Helper in MVC, it’s very basic and just displays a string where ever I use it. So it’s a .cshtml file in my App_Code folder, I think that is how it's supposed to be set up, with the following code in it,
@helper DisplaySelect() {
@:This text is coming from an helper class.
}
Now I am a wiz with helpers how do I make it do things. E.g.. say I want it to query the database and display something, I would normally do that work in my controller. How do I do that with helpers, do I create a helper controller and then treat the helper like a partial view???
Any help would be greatly appreciated.
Cheers,
Mike.
Thanks guys,
I’ve asked that question before Shark “Why would you use a helper and not a partial view” the answer I got there was a partial view is more for just displaying common HTML where as a Helper can have a bunch of code in it and do all kinds of great processing stuff. Now it seems that’s not true and they are pretty much the same thing, in some respect, except in Link664’s case!
I like what you’re saying Link664 that makes sense as it cleans up the code nicely.
What I was going to do was try and populate a drop down list in a helper and then use it in multiple places, but from my research today that’s not what helpers are for.
Cheers,
Mike.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对你想做什么以及为什么要这样做有点困惑,但我会尝试一下。正如您所提到的,您通常会在控制器中完成这项工作。从客户端创建的部分视图/html 帮助程序进行数据库调用是非常糟糕的设计。
@helper
语法仅用于简化视图代码,而不用于实现应位于视图中的控制器或模型中的代码。请参阅 这篇文章可以更好地了解何时应该使用它们。至于您的示例,我建议您从控制器传递所需的数据,并在 HtmlHelper 上创建一个扩展方法以将其呈现在您的视图中。例如,如果您想在您的视图上显示最近 10 篇帖子的列表:
在您的控制器中
然后在您的视图中您可以输入
希望这就是您正在寻找的内容,如果不更新您的问题,那么它就不会那么含糊了我会再试一次!
I'm a bit confused by what you want to do and why you want to do it but I'll give it a go. As you mentioned, you would normally do that work in your controller. It is very poor design to make database calls from a partial view/html helper created client-side.
The
@helper
syntax is to be used only for simplifying view code, not for implementing code that should be in the controllers or models in the view. See this article for a better idea of when you should be using them.As for your example, I recommend you pass the data that you want from your controller and create an extension method on HtmlHelper to render it in your view. For example if you wanted to display a list of the most recent 10 posts on your view:
and in your controller
Then in your view you can put
Hopefully that is what you are looking for, if not update your question so it's not so ambiguous and I'll try again!