【问题标题】:MVC 5.1 - Adding Html attributes in EditorFor with customized Editor TemplatesMVC 5.1 - 使用自定义编辑器模板在 EditorFor 中添加 Html 属性
【发布时间】:2014-10-01 10:04:47
【问题描述】:

我可以添加 htmlAttributes 如下所示,并以 Html 呈现

@Html.EditorFor(Function(model) model.LastName, New With {Key .htmlAttributes = New With {Key .ng_model = "model.LastName"}})

但是当我在Views\Shared\EditorTemplates\Date.vbhtml 中添加了日期数据类型的自定义编辑器模板如下

@Code
    Dim value = ""
    If Not Model Is Nothing Then
        value = String.Format("{0:d}", Model.Value.ToString("dd/MM/yyyy"))
    End If
    @Html.TextBox("", value, New With {Key .class = "datefield", Key .type = "text"})
End Code

我使用 EditorFor 和 htmlAttributes,然后 HtmlAttributes 没有呈现?

【问题讨论】:

  • 发布渲染的内容而不是告诉它没有渲染可能是个好主意。

标签: asp.net-mvc razor asp.net-mvc-5.1


【解决方案1】:

这是我用于自定义 DateTime 编辑器模板的代码示例。我能够访问传递给 editorfor 的 html 属性。希望这会有所帮助。

@model DateTime

@{
    object htmlAttributes = null;
    if (ViewContext.ViewData.Keys.Contains("htmlAttributes"))
    {
        htmlAttributes = ViewContext.ViewData["htmlAttributes"];
    }
}

@Html.TextBoxFor(model => model, htmlAttributes)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 2011-11-08
    • 2012-02-26
    • 2014-05-04
    • 1970-01-01
    相关资源
    最近更新 更多