ASP.NET MVC 从视图引用 Html Helper
我编写了一个名为 DetailsForm 的 Html Helper,以减少在视图中显示字段时的重复。在视图中 - 实际上是部分视图,ascx 文件 - 我可以像这样引用它:
Storyboard.Helpers.DetailsForm.LabelAndData(Html, m => m.id)
但想像这样引用它:
LabelAndData(Html, m => m.id)
ascx 文件中似乎不允许使用“using”指令。有同等的吗?
I've written an Html Helper called DetailsForm to reduce repetition when displaying fields in a view. Within a view - actually a partial view, ascx file - I can refer to it like this:
Storyboard.Helpers.DetailsForm.LabelAndData(Html, m => m.id)
But would like to refer to it like this:
LabelAndData(Html, m => m.id)
A 'using' directive doesn't seem to be allowed in the ascx file. Is there an equivalent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你应该能够在你的ascx顶部导入命名空间,如下所示:
更新
阅读你的评论,我猜你的助手看起来像这样:
在这种情况下,你会这样称呼它您的看法:
希望有帮助!
you should be able to import the namespace in the top of your ascx, like so:
UPDATE
Reading your comment, I'm guessing your helper looks something like:
in which case you would call it like this in your view:
Hope that helps!
如果您使用与您创建的默认 Html Helpers 相同的命名空间(即 MVC 的 System.Web.Mvc.Html),则无需在任何地方引用您创建的 Html Helper。
if you use the same namespace as the Default Html Helpers which is
System.Web.Mvc.Html
of MVC for the one that you created, you don't need to reference your created Html Helper anywhere.