我如何编辑Html.DisplayFor方法的CSS在MVC 3?编辑、方法、Html、DisplayFor

2023-09-04 02:48:50 作者:旧人旧梦旧悲伤

我使用下面的code,显示在我看来,从我的观点示范文本:

  @ Html.DisplayFor(M => m.Name)
 

当我看看IE9的HTML细节(这是我在工作中使用),没有与这个名字相关联的类,它只是使用了车身CSS样式,而不是显示字段级的造型。有谁知道什么可能会造成这个问题或如何我会编辑CSS文本产生的?

解决方案

DisplayFor用于模板的原因。如果不使用模板,那么你应该使用像这样的项目: @ Model.Name 如果你想给它一个类或ID,那么你需要包裹在一个跨度或股利。

您的问题是,你使用了错误的方法来输出数据,并期待它做些别的事情。没有内置的方式来输出原始数据与类名。

所以,你的选择是,在包装容器的原始项目,你可以将CSS应用到,或创建一个模板使用这些,那么在DisplayFor像这样指定模板名称:

  @ Html.DisplayFor(M => m.Name,NameTemplate)
 
音频工具软件绿色版下载

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?

解决方案

DisplayFor is used for templating reasons. 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.

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:

 @Html.DisplayFor(m => m.Name, "NameTemplate")