【问题标题】:Issue in calling a Custom Html Helper from another从另一个调用自定义 Html 助手的问题
【发布时间】:2021-09-07 01:47:23
【问题描述】:

我有一个名为 DisplaySumForCompany 的自定义辅助方法,如下所示:

public static class MvcExtension
{
   public static MvcHtmlString DisplaySumForCompany<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, int timeFrameTypeIndex, Expression<Func<TModel, TValue>> expression, long definitionId, string popupHeader)
   where TModel : IKeyPush
    {
      // lines of code
    }

    // lines of code
}

我需要从我在同一个类中创建的另一个自定义助手方法调用此方法;我试着这样称呼它:

public static MvcHtmlString ConstructCollapsibleColumns(this HtmlHelper htmlHelper, int columnIndex)
{
   // lines of code
   DisplaySumForCompany(0, x => x.Children[j].SummationNodeDetailList[model.Children[j].SummationNodeDetail(0).TimeFrameType].DiscreteItemsA, 0, "");
   // lines of code
    }

当我这样做时,我收到以下错误:

我相信这是因为我没有传递第一个参数(上面突出显示),或者换句话说,它假设我传递的第一个参数是htmlHelper。我的问题是我如何通过它(如果有的话)?你能告诉我哪里出错了吗?

注意,当我从 View(cshtml) 页面进行相同的调用时,没有错误:

@Html.DisplaySumForCompany(0, x => x.Children[j].SummationNodeDetailList[Model.Children[j].SummationNodeDetail(0).TimeFrameType].DiscreteItemsA, 0, "") 

更新

当我尝试传递htmlHelper作为参数即DisplaySumForCompany(htmlHelper, 0, x =&gt; x.Children[j].SummationNodeDetailList[model.Children[j].SummationNodeDetail(0).TimeFrameType].DiscreteItemsA, 0, "");时,它会抛出这个错误:

【问题讨论】:

    标签: c# asp.net asp.net-mvc razor


    【解决方案1】:

    您可以将htmlHelper 作为参数传递给嵌套的 DisplaySumForCompany 方法,如下所示。

    DisplaySumForCompany(htmlHelper, 0, x =&gt; x.Children[j].SummationNodeDetailList[model.Children[j].SummationNodeDetail(0).TimeFrameType].DiscreteItemsA, 0, "");

    您也可以调用扩展方法,因为您已经拥有 IHtmlHelper 的实例,例如

    htmlHelper.DisplaySumForCompany(0, x =&gt; x.Children[j].SummationNodeDetailList[model.Children[j].SummationNodeDetail(0).TimeFrameType].DiscreteItemsA, 0, "");

    【讨论】:

    • stackoverflow.com/users/3636674/richmercer 会抛出错误;我在我的问题中更新了同样的内容
    • 如果把它作为扩展方法调用呢? htmlHelper.DisplaySumForCompany(0, x =&gt; x.Children[j].SummationNodeDetailList[model.Children[j].SummationNodeDetail(0).TimeFrameType].DiscreteItemsA, 0, "");
    猜你喜欢
    • 2016-09-02
    • 2018-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-18
    相关资源
    最近更新 更多