【问题标题】:How to refer to a CSS style using ASP.NET MVC?如何使用 ASP.NET MVC 引用 CSS 样式?
【发布时间】:2012-04-10 16:20:22
【问题描述】:

在 ASP.NET MVC 中,我可以定义一个像这样的文本框编辑器并给它一个样式。

 @Html.TextBoxFor(m => m.Notes[i].Notes, new { style = "width: 500px;" });

如何将样式移动到 Site.css 文件中,然后从上面的代码中引用它?

.myStyle {
    width: 500px;
}

我试过这个编译但似乎不起作用:

@Html.TextBoxFor(m => m.Notes[i].Notes, "myStyle");

【问题讨论】:

    标签: css asp.net-mvc asp.net-mvc-3 asp.net-mvc-4


    【解决方案1】:

    你想给它一个 class 属性来匹配你的 CSS 规则:

    @Html.TextBoxFor(m => m.Notes[i].Notes, new { @class = "myStyle" });
    

    请注意,@class 中的@ 在 ASP.NET MVC 中没有特殊含义。它只是将 C# 中的关键字 class 转换为标识符,因此您可以将其作为属性传递并编译。

    【讨论】:

      【解决方案2】:

      一句话解释。通常,如果您想添加属性,例如只读,你可以输入:

      @Html.TextBoxFor(m => m.Notes[i].Notes, new { readonly = "readonly" });
      

      注意 readonly 前面没有@。您必须将@ 放在类属性前面,因为它是C# 中的关键字。如果您在 VB.NET 中执行此操作,则不必转义,因为您使用前导 . 定义属性:

      @Html.TextBoxFor(Function(m) m.Notes[i].Notes, New With { .class = "myStyle" });
      

      【讨论】:

      • New With... 我今天学到的另一个很酷的 VB.NET 语言功能。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-26
      • 2011-02-08
      • 2016-05-31
      • 2011-01-07
      • 1970-01-01
      • 1970-01-01
      • 2016-07-05
      相关资源
      最近更新 更多