C# 如何在 MVC 3 中编辑 Html.DisplayFor 方法的 CSS?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11655810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:41:34  来源:igfitidea点击:

How do I edit the CSS of the Html.DisplayFor method in MVC 3?

c#.netcssasp.net-mvc-3

提问by barnone

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. Does anyone know what might be causing this issue or how I might edit the CSS for the text created?

当我查看 IE9 中的 HTML 详细信息(我必须在工作中使用)时,没有与名称关联的类,它只使用 Body CSS 样式而不是显示字段类样式。有谁知道是什么导致了这个问题,或者我如何为创建的文本编辑 CSS?

采纳答案by Erik Funkenbusch

DisplayFor is used for templating reasons. If you aren't using a template, then you should just use the item like so: @Model.NameIf you want to give it a class or id, then you need to wrap it in a span or div.

DisplayFor 用于模板化原因。如果您没有使用模板,那么您应该像这样使用项目:@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")

回答by Dmitry Efimenko

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:

否则将它放在一个带有类的跨度中并为该类更新 css:

your html:

你的html:

<span class="name">
    @Html.DisplayFor(m => m.Name)
</span>

your css:

你的CSS:

.name {
    //custom css
}

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):

在“视图”>“共享”>“显示模板”中(如果您还没有此文件夹,请创建它):

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.

除了字符串之外,您可能还需要为其他提示添加 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

更新

Explanation:

解释:

There is an overload on Html.DisplayForwhich accepts two parameters: expression and object additionalViewData. So the second parameter that I pass is that anonymous object additionalViewData. I create this object with property called class

有一个Html.DisplayFor接受两个参数的重载:表达式和object additionalViewData。所以我传递的第二个参数是匿名对象 additionalViewData。我用名为的属性创建了这个对象class

Inside of the html helper I then check if there is a ViewData with a key classand if there is, I put output inside a span with that class value.

在 html helper 内部,然后我检查是否有带有键的 ViewData,class如果有,我将输出放在具有该类值的范围内。

** updated variable name from classto myClasssince "class" is not appropriate variable name.

** 更新了变量名classmyClass因为“类”不是合适的变量名。