通过 Htmlhelper.editorfor() (MVC) 创建颜色输入类型

Creating a color inputtype through Htmlhelper.editorfor() (MVC)

我有使用 htmlhelper 创建的表单的以下部分:

    <div class="form-group">
        @Html.LabelFor(model => model.ColorCode, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.ColorCode, new {htmlAttributes = new { @class = "form-control" } })
            <!--<input type="color" class="form-control"/>-->
            @Html.ValidationMessageFor(model => model.ColorCode, "", new {@class = "text-danger"})
        </div>
    </div>

ColorCode的数据类型是字符串,所以默认情况下它只是创建一个普通的文本框。我想要的是让输入充当颜色选择器。注释 html 本质上是我希望它创建的内容(+ 将其连接到表单所需的任何属性,如果适用。我不是 HTML 专家)。有没有办法做到这一点?我无法通过搜索找到有关它的任何信息。

您可以尝试这样的操作:

@Html.TextBoxFor(model => model.ColorCode, new {@class="form-control", type="color"})

EditorFor 将使用编辑器模板生成 html 输出。要使用它,您必须为特定的 属性 类型定义一个编辑器模板,但由于这是 string 它有点通用。

@Stephen Muecke 在评论中也指出了这一点。