簡體   English   中英

MVC 5.1-使用自定義的編輯器模板在EditorFor中添加HTML屬性

[英]MVC 5.1 - Adding Html attributes in EditorFor with customized Editor Templates

我可以添加如下的htmlAttributes及其在HTML中呈現

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

但是,當我在Views\\Shared\\EditorTemplates\\Date.vbhtml為Date數據類型添加自定義編輯器模板時,如下所示

@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

並且我將htmlAttributes與EditorFor一起使用,則HtmlAttributes無法呈現嗎?

這是我用於自定義DateTime編輯器模板的代碼示例。 我能夠訪問傳遞到editorfor的html屬性。 希望這可以幫助。

@model DateTime

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM