【问题标题】:Creating a color inputtype through Htmlhelper.editorfor() (MVC)通过 Htmlhelper.editorfor() (MVC) 创建颜色输入类型
【发布时间】:2017-10-12 11:52:53
【问题描述】:

我使用 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(m =&gt; m.ColorCode, new { @class = "form-control", type="color" })

标签: asp.net-mvc html html-helper


【解决方案1】:

你可以试试这样的:

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

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

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

【讨论】:

  • 这很好用。实际上,我尝试过但失败的最初解决方案也有效。但我更喜欢你的,因为有了它我可以继续使用@Html.-格式。问题实际上是我在后端将颜色代码的类型从 int 更改为字符串(以便更轻松地处理来自颜色选择器类型的输入),但我忘记更新我的 API 的一部分以将其构建为一个字符串而不是一个 int。无论如何,谢谢!
猜你喜欢
  • 1970-01-01
  • 2012-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-16
  • 2014-04-18
  • 1970-01-01
  • 2017-06-26
相关资源
最近更新 更多