Addind CSS类剃刀元素 [英] Addind css classes to razor elements
问题描述
在剃刀,如果我有这样的事情:
In razor if I had something like:
@ Html.EditorFor(型号=> model.name)
甚至 @ Html.CheckBoxFor(M = GT; m.RememberMe )
我将如何添加一个CSS类或ID给他们?我一直在阅读有关佣工,但我必须做出一个帮手每一个元素?有没有简单的方法来添加一个类或ID来剃刀表单元素?
How would I add a css class or an id to them? I have been reading about helpers, but would I have to make a helper for every single element? is there no easy way to add a class or id to razor form elements?
推荐答案
您不能做到这一点与 EditorFor
助手只是因为你不知道会用什么模板。你可以实现它,但你需要编写一个自定义编辑模板。例如,这可以通过重写来实现的<一个href=\"http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html\"相对=nofollow> 默认编辑模板
并考虑到其重新$ P $,第二个参数psents额外的视图的数据。
You cannot do that with the EditorFor
helper simply because you don't know what template will be used. You could achieve it but you will need to write a custom editor template. For example this could be achieved by overriding the default editor template
and taking into account the second parameter which represents an additional view data.
下面是这样一个自定义编辑器模板如何查找字符串类型(〜/查看/共享/ EditorTemplates / string.cshtml
)的例子:
Here's an example of how such a custom editor template could look for string types (~/Views/Shared/EditorTemplates/string.cshtml
):
@Html.TextBox(
"",
ViewData.TemplateInfo.FormattedModelValue,
ViewData
)
然后你可以使用它像:
and then you could use it like that:
@Html.EditorFor(model => model.name, new { @class = "myclass" })
随着CheckBoxFor帮手,你能做到这一点:
With the CheckBoxFor helper you could do that:
@Html.CheckBoxFor(m => m.RememberMe, new { @class = "myclass" })
这篇关于Addind CSS类剃刀元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!