[英]How do I edit the CSS of the Html.DisplayFor method in MVC 3?
I am using the following code to display text from my view model in my view: 我使用以下代码在我的视图中显示视图模型中的文本:
@Html.DisplayFor(m => m.Name)
When I look at the HTML details in IE9 (which I have to use at work) there is no class associated with the name, it just uses the Body CSS styling instead of the display-field class styling. 当我查看IE9中的HTML细节(我必须在工作中使用)时,没有与该名称相关联的类,它只使用Body CSS样式而不是display-field类样式。 Does anyone know what might be causing this issue or how I might edit the CSS for the text created? 有谁知道可能导致此问题的原因或我如何编辑创建的文本的CSS?
if it is a label, use proper helper for it as Nataka526 suggests 如果它是一个标签,请使用适当的帮助,因为Nataka526建议
otherwise put it in a span with a class and update css for that class: 否则将它放在带有类的span中并更新该类的css:
your html: 你的HTML:
<span class="name">
@Html.DisplayFor(m => m.Name)
</span>
your css: 你的css:
.name {
//custom css
}
UPDATE UPDATE
Another option: Update your Display Templates to handle a specific ViewData key: 另一个选项:更新显示模板以处理特定的ViewData键:
in Views > Shared > DisplayTemplate (create this folder if you don't have it already): 在Views> Shared> DisplayTemplate中(如果您还没有,请创建此文件夹):
add file String.cshtml: 添加文件String.cshtml:
@model string
@{
string myClass = ViewData["class"]
}
@if(string.IsNullOrEmpty(myClass))
{
@:@Model
}
else
{
<span class="@myClass">@Model</span>
}
you may need to add DisplayTemplates for other tipes as well besides string. 除了字符串之外,您可能还需要为其他tipes添加DisplayTemplates。
In the view you will write something like this: 在视图中,您将编写如下内容:
@Html.DisplayFor(m => m.Name, new { @class= "name" })
This will add spans around it automatically. 这将自动添加围绕它的跨度。
UPDATE UPDATE
Explanation: 说明:
There is an overload on Html.DisplayFor
which accepts two parameters: expression and object additionalViewData
. Html.DisplayFor
上有一个重载,它接受两个参数:expression和object additionalViewData
。 So the second parameter that I pass is that anonymous object additionalViewData. 所以我传递的第二个参数是匿名对象additionalViewData。 I create this object with property called class
我用名为class
属性创建此对象
Inside of the html helper I then check if there is a ViewData with a key class
and if there is, I put output inside a span with that class value. 在html帮助器内部,然后检查是否存在具有键class
的ViewData,如果有,我将输出放在具有该类值的span中。
** updated variable name from class
to myClass
since "class" is not appropriate variable name. **更新了从class
到myClass
变量名,因为“class”不是合适的变量名。
DisplayFor is used for templating reasons. DisplayFor用于模板原因。 If you aren't using a template, then you should just use the item like so: @Model.Name
If you want to give it a class or id, then you need to wrap it in a span or div. 如果你没有使用模板,那么你应该只使用这样的项目: @Model.Name
如果你想给它一个类或id,那么你需要将它包装在span或div中。
Your problem is that you're using the wrong method to output data, and expecting it to do something else. 您的问题是您使用错误的方法输出数据,并期望它做其他事情。 There is no built-in way to output raw data with class names. 没有内置的方法来输出带有类名的原始数据。
So your choices are, wrap the raw item in a container that you can apply the css to, or create a template to use for these, then specify the template name in the DisplayFor like so: 所以你的选择是,将原始项包装在可以应用css的容器中,或者创建一个用于这些的模板,然后在DisplayFor中指定模板名称,如下所示:
@Html.DisplayFor(m => m.Name, "NameTemplate")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.