EditorTemplate(@Html.EditorFor)传递 HTML属性值

场景:

EditorTemplate,给模版里面的html元素增加class属性,

 

分析:

我的使用@Hhtml.EditorFor()的时候,如果我们使用系统默认模版(即我们没有增加相应模版)时,在网上搜的方法如下,

@Html.EditorFor(m=>m.Name,new{htmlAttributes=new{@class="form-control"}})

那我们在定义模版时,将htmlAttributes属性取出即可.

 

实现:

模版内正确的定义方法是:

@Html.TextBox("", @ViewData.TemplateInfo.FormattedModelValue, @ViewData["htmlAttributes"])

使用方法就是:
@Html.EditorFor(m=>m.Name,new{htmlAttributes=new{@class="form-control"}})

posted @ 2018-04-07 18:12  cheneyx  阅读(1161)  评论(0编辑  收藏  举报