如何在 razor @helper 中使用 HtmlHelper 和 AjaxHelper?

发布于 2024-11-19 01:28:53 字数 2063 浏览 2 评论 0原文

我想创建一个助手:

@using System.Web.Mvc
@using System.Web.Mvc.Ajax
@using System.Web.Mvc.Html
@using System.Web.Routing
@using Proj.Extenders
@using Proj.Web.Mvc
@using Proj.Web.Mvc.Paging
@using Proj.Mvc.Helpers
@using Proj.Mvc

@helper Create(dynamic ajaxHelper, dynamic htmlHelper,
    string text,
    string targetID,
    string actionName,
    string controllerName,
    object routeValues,
    string active)
{
    <li@{ if (active == targetID) { <text> class="active"</text> } }>
        @ajaxHelper.ActionLink(htmlHelper.Resource(text).ToString(),
            actionName, controllerName, routeValues, updateTargetId: targetID)
    </li>
}

问题是我在 HtmlHelper 上有扩展方法,其中 T 是模型的类型。我想对多个视图使用相同的帮助器,但视图上的 @model 会有所不同。

它没有找到扩展方法:

'System.Web.Mvc.AjaxHelper<X>' does not contain a definition for 'ActionLink'

ActionLink 方法

public static MvcHtmlString ActionLink<T>(this AjaxHelper<T> ajax,
    string linkText = " ",
    string actionName = null, string controllerName = null,
    object routeValues = null, object htmlAttributes = null,
    string confirm = null,
    string httpMethod = null,
    InsertionMode insertionMode = InsertionMode.Replace,
    int loadingElementDuration = 0,
    string loadingElementId = null,
    string onBegin = null,
    string onComplete = null,
    string onFailure = null,
    string onSuccess = null,
    string updateTargetId = null,
    string url = null)
{
    var options = GetAjaxOptions(confirm, httpMethod, insertionMode, loadingElementDuration,
        loadingElementId, onBegin, onComplete, onFailure, onSuccess, updateTargetId, url);

    return ajax.ActionLink(linkText, actionName, controllerName, routeValues, options, htmlAttributes);
}

该文件位于

- Website
  - App_Code
     TabHelper.cshtml

如何使用 HtmlHelperAjaxHelper 方法在帮助文件上?

I want to create a helper:

@using System.Web.Mvc
@using System.Web.Mvc.Ajax
@using System.Web.Mvc.Html
@using System.Web.Routing
@using Proj.Extenders
@using Proj.Web.Mvc
@using Proj.Web.Mvc.Paging
@using Proj.Mvc.Helpers
@using Proj.Mvc

@helper Create(dynamic ajaxHelper, dynamic htmlHelper,
    string text,
    string targetID,
    string actionName,
    string controllerName,
    object routeValues,
    string active)
{
    <li@{ if (active == targetID) { <text> class="active"</text> } }>
        @ajaxHelper.ActionLink(htmlHelper.Resource(text).ToString(),
            actionName, controllerName, routeValues, updateTargetId: targetID)
    </li>
}

The problem is that I have extension methods on the HtmlHelper<T> where T is the type of the model. I want to use the same helper for several views, but the @model on the views will be different.

It is not finding the extension method:

'System.Web.Mvc.AjaxHelper<X>' does not contain a definition for 'ActionLink'

ActionLink method

public static MvcHtmlString ActionLink<T>(this AjaxHelper<T> ajax,
    string linkText = " ",
    string actionName = null, string controllerName = null,
    object routeValues = null, object htmlAttributes = null,
    string confirm = null,
    string httpMethod = null,
    InsertionMode insertionMode = InsertionMode.Replace,
    int loadingElementDuration = 0,
    string loadingElementId = null,
    string onBegin = null,
    string onComplete = null,
    string onFailure = null,
    string onSuccess = null,
    string updateTargetId = null,
    string url = null)
{
    var options = GetAjaxOptions(confirm, httpMethod, insertionMode, loadingElementDuration,
        loadingElementId, onBegin, onComplete, onFailure, onSuccess, updateTargetId, url);

    return ajax.ActionLink(linkText, actionName, controllerName, routeValues, options, htmlAttributes);
}

The file is located at

- Website
  - App_Code
     TabHelper.cshtml

How can I use HtmlHelper<T> and AjaxHelper<T> methods on the helper file?

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

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

发布评论

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

评论(1

假情假意假温柔 2024-11-26 01:28:53

内联 @helper 不能是通用的。您将必须使用定义为扩展方法的标准帮助器,或者只是一些编辑器/显示模板或部分视图。在您的特定情况下,我不明白为什么您的自定义 ActionLink 必须是通用的。您最终会在最后调用 ajax.ActionLink ,这不是通用的。

Inline @helper cannot be generic. You will have to use either a standard helper defined as an extension method or or simply some editor/display template or a partial view. This being said in your particular case I don't see why your custom ActionLink must be generic. You end up calling ajax.ActionLink at the end which is not generic.

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