静态类型不能用作 htmlhelpers 中的参数

static types cannot be used as parameters in htmlhelpers

我创建了这样的自定义 HtmlHelper:

public static class HtmlHelper
{
    public static MvcHtmlString CreateHr(this HtmlHelper helper)
    {
        return MvcHtmlString.Create("<div class='line'></div>");
    }
}

但是当我构建项目时我得到这个错误:

static types cannot be used as parameters

我搜索了Google,但找不到类似的问题。 我该怎么做?

将您的 class 称为 HtmlHelper 以外的名称,例如 HtmlHelperExtensions

您不仅不允许将静态 class 作为方法参数类型(因为它没有任何意义),而且您会掩盖您正在尝试的 class延长。

不要将您的助手命名为 HtmlHelper,因为这意味着您试图从 MVC 框架中隐藏基础 HtmlHelper。而是尝试:

using System;
using System.Web.Mvc;

namespace MvcApplication1.Helpers
{
     public static class HrExtensions
     {
          public static string CreateHr(this HtmlHelper helper)
          {
               return MvcHtmlString.Create("<div class='line'></div>");
          }
     }
}

此处有更多详细信息:http://www.asp.net/mvc/overview/older-versions-1/views/creating-custom-html-helpers-cs