Mvc 助手调用

发布于 2024-12-29 21:15:35 字数 505 浏览 4 评论 0原文

我正在编写一些自定义助手,当前的助手涉及一种“数据网格控件”类型的助手,我似乎遇到了一些障碍。

如果我处于剃刀视图中,我可能会执行类似 Html.EditorFor(someExpression) 的操作,但我似乎找不到在自定义帮助程序中的帮助程序代码上下文中执行此操作的方法。

实际上,我正在尝试从助手内部调用助手。

这是我能想到的最基本形式的示例:

public static MvcHtmlString Test(dynamic Model)
{
   return new MvcHtmlString( Html.textBox(Model.SomeProperty) )
}

有什么想法吗?

当我在代码中添加这样的 using 语句时,我发现这是多么愚蠢......

using Html = System.Web.Mvc.Html;

谈论简单......自我注意......注意框架!

I'm writing some custom helpers the current one involves a sort of "datagrid control" type helper and i seem to have hit a bit of a hurdle.

if i was in a razor view i coud something like Html.EditorFor(someExpression) and i can't seem to find a way to do that within the context of my helper code within my custom helper.

Effectively i'm trying to call a helper from within a helper.

Here's an example in the most basic form i can think of:

public static MvcHtmlString Test(dynamic Model)
{
   return new MvcHtmlString( Html.textBox(Model.SomeProperty) )
}

Any ideas ?

I figured out how dumb this was when I added a using statement like this to my code ...

using Html = System.Web.Mvc.Html;

Talk about simple ... note to self ... pay attention to framework !!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乄_柒ぐ汐 2025-01-05 21:15:35

将您的辅助方法改为扩展方法。

public static MvcHtmlString Test(this HtmlHelper html, dynamic Model)
{
   return new MvcHtmlString( html.textBox(Model.SomeProperty) )
}

您可以通过首先使用方法所在的命名空间(类)来调用此方法,然后只需 Html.Test(Model)

Make your helper method into a extension method instead.

public static MvcHtmlString Test(this HtmlHelper html, dynamic Model)
{
   return new MvcHtmlString( html.textBox(Model.SomeProperty) )
}

You call this by first have a using of the namespace where (the class where) the method is and then just Html.Test(Model)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文