How to refer to a CSS style using ASP.NET MVC?


Tags: css,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-4

Problem :

In ASP.NET MVC I could define a textBox editor like this and give it a style.

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

How can I move the style to the Site.css file and just refer to it from the code above?

.myStyle {
    width: 500px;
}

I tried this which compiles but doesn't seem to work:

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


Solution :

You want to give it a class attribute for your CSS rule to match:

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

Note that the @ in @class has no special meaning in ASP.NET MVC. It's simply there to turn class, a keyword in C#, into an identifier, so you can pass it in as a property and it'll compile.


    CSS Howto..

    How to visually indicate current page in ASP.NET MVC?

    How to shift element upwards to it's variable height using css

    How to use jQuery to set min-height the same as window

    Does Modernizr.js help in showing css 3 and html 5 on older browsers and how?

    How to make CSS menu bar activated only after my mouse cursor is at the menu.

    How to draw a shape using a piece of image in php

    How to create responsive text on top of an image?

    Rollover table shows hidden span class text with css

    How to reorganize the page using “div” and “css”?

    Ionic: how to use cssClass in $ionicPopup?

    How to make expand-collapse different div heights according to device width using css/javascript

    Menu - Show up the Submenuitems under the whole Menuitems - HTML/CSS

    how to crop big image in bootstrap carousel

    How can I use the HTML data attribute in CSS as a property? [duplicate]

    How do you square off only one corner of a div to be transparent in CSS?

    css/jquery - how to hide last row of floated elements if not even

    using a simple list item
  • what am I doing wrong. background color doesnt show up
  • How to make my .css and .js file work with my html code loaded in a WebView on Android

    How to stop text from vertically centering in a table?

    How to do fallback for browser-specific css expressions

    How to simulate \hfill with HTML and CSS?

    CSS IE7 How can I force UL inside LI to be wider than the parent

    How do I get a button to show on mouseover using jQuery?

    CSS/HTML - How do I get text to go behind an image inside a div

    CSS - How to float correctly into pseudocolumns

    How to have a textbox with label inside it support 'tap to paste' feature in iPad?

    Yahoo news CSS, how to achieve behavior

    How can I use CSS for Vaadin components?

    How to automatically style HTML output of kramdown converter?

    All CSS menu when hover show submenu