Asp.net mvc MVC 5.1-使用自定义编辑器模板在EditorFor中添加Html属性
我可以添加htmlAttributes,如以下内容及其在Html中的呈现Asp.net mvc MVC 5.1-使用自定义编辑器模板在EditorFor中添加Html属性,asp.net-mvc,razor,asp.net-mvc-5.1,Asp.net Mvc,Razor,Asp.net Mvc 5.1,我可以添加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 val
@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不呈现?这是我用于自定义DateTime编辑器模板的代码示例。我能够访问传递到editorfor的htmlattributes。希望这有帮助
@model DateTime
@{
object htmlAttributes = null;
if (ViewContext.ViewData.Keys.Contains("htmlAttributes"))
{
htmlAttributes = ViewContext.ViewData["htmlAttributes"];
}
}
@Html.TextBoxFor(model => model, htmlAttributes)
也许一个好主意是发布渲染的内容,而不是说它不是渲染。