无法自举类应用到我的EditorFor [英] unable to apply Bootstrap classes to my EditorFor
问题描述
我工作的一个asp.net MVC-5的Web应用程序,我写了以下内容: -
I am working on an asp.net mvc-5 web application , and i wrote the following :-
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
&
@Html.EditorFor(model => model.Name, new { @class = "form-control" })
但是这不会对生成的HTML任何影响,但如果我改变了 EditorFor
为T extboxFor
我会得到表单控制类的效果?可以在此请任何人的意见?我读这仅支持内部asp.net mvc的5.1?那么,什么是可用的方法,我可以跟着得到这个工作以外我的asp.net mvc的升级-5到asp.net的MVC 5.1,以消除升级的风险?
谁能书于?
but this will not have any effect on the generated html, but if i changed the EditorFor
to be TextboxFor
i will get the effect for the form-control class ? can anyone advice on this please ? i read that this is supported only inside asp.net mvc 5.1 ? so what are the available approaches i can follow to get this working other than upgrading my asp.net mvc-5 to asp.net mvc 5.1 , to eliminate the risk of upgrading?
Can anyone adivce ?
推荐答案
是,通过HTML属性为标准 EditorFor()
你需要MVC-5.1 +。如果你想用MVC-5复制这一点,你可以创建一个自定义编辑器模板,并使用接受 additionViewData code>超载传递的属性。例如,创建一个新的
EditorTemplate
名为String.cshtml应用模板是typeof运算字符串
的所有属性
Yes, to pass html attributes to a standard EditorFor()
you need MVC-5.1+. If you want to replicate this with MVC-5, you can create a custom editor template and pass the attributes using the overload that accepts additionViewData
. For example, create a new EditorTemplate
named "String.cshtml" to apply the template for all properties that are typeof 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
属性为您的意见如上所述,例如:
The second example shows how to respect the DisplayFormat
attribute as mentioned in your comments above, for example
[DisplayFormat(DataFormatString="{0:C}", ApplyFormatInEditMode = true)]
public decimal Amount { get; set; }
将值格式化为货币字符串。
will format the value as a currency string.
<一个href=\"http://stackoverflow.com/questions/26162218/editortemplate-for-dropdownlist/26417466#26417466\">This回答也给一些其他的选项,包括用于渲染引导控制创建自定义的HTML帮助
This answer also gives some other options including creating a custom html helper for rendering bootstrap controls
这篇关于无法自举类应用到我的EditorFor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!