我正在开发一个asp.net mvc-5 web应用程序,我编写了以下内容:
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
&
@Html.EditorFor(model => model.Name, new { @class = "form-control" })但是这不会对生成的html产生任何影响,但是如果我将EditorFor更改为TextboxFor,我会得到表单控制类的效果吗?有人能对此提出建议吗?我读到只有在asp.net mvc 5.1中才支持这一点?那么,除了将我的asp.net mvc-5升级到asp.net mvc 5.1之外,我还可以遵循哪些可用的方法来实现这个工作,以消除升级的风险呢?有人能放弃吗?
发布于 2015-01-17 02:59:39
是的,要将html属性传递给标准的EditorFor(),您需要MVC-5.1+。如果您想要用MVC-5复制这一点,您可以创建一个自定义编辑器模板,并使用接受additionViewData的重载传递属性。例如,创建一个名为“EditorTemplate”的新String.cshtml,为所有类型为string的属性应用模板
/Views/Shared/EditorTemplates/String.cshtml
@Html.TextBoxFor(m => m, ViewData["attributes"])在视野中
@Html.EditorFor(m => m.Name, new { attributes = new { @class = "form-control" } })或者创建一个特定的EditorTemplate
/Views/Shared/EditorTemplates/MyCustomTemplate.cshtml
@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, ViewData["htmlAttributes"])在视野中
@Html.EditorFor(m => m.Name, "MyCustomTemplate", new { attributes = new { @class = "form-control" } })第二个示例展示了如何尊重上面的注释中提到的DisplayFormat属性,例如
[DisplayFormat(DataFormatString="{0:C}", ApplyFormatInEditMode = true)]
public decimal Amount { get; set; }将值格式化为货币字符串。
This answer还提供了其他一些选项,包括创建用于呈现引导控件的自定义html帮助程序。
发布于 2017-09-09 09:09:24
这样做是可行的:
@Html.EditorFor(x => x.Created, new { htmlAttributes = new { @class = "date" } })https://stackoverflow.com/questions/27975810
复制相似问题