4

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");

2 Answers 2

11

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.

Sign up to request clarification or add additional context in comments.

Comments

1

One word of explanation. Normally if you want to add attributes, e.g. readonly, you would type:

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

Notice there is no @ in front of readonly. You have to put @ in front of the class attribute, because it's a keyword in C#. If you do it in VB.NET you do not have to escape, because you define properties with a leading .:

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

1 Comment

New With... another cool VB.NET language feature I learned today.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.