1

I would like to add multiple css classes to @Html.Label for but it does not

I have already tried the following:

   @Html.LabelFor(model => model.ServiceName, htmlAttributes: new { @class = "SerivcesRegistrationLabel" , "control-label col-md-2" })


   @Html.LabelFor(model => model.ServiceName, htmlAttributes: new { @class = "SerivcesRegistrationLabel"  "control-label col-md-2" })

Both of these give me errors

2 Answers 2

3

Place all of the class names in one string, each separated by a space. Like:

@class = "SerivcesRegistrationLabel control-label col-md-2"
Sign up to request clarification or add additional context in comments.

Comments

1

Close. All attributes on Html helpers like @id and @class kind of simulate how the string will actually show up as an attribute, without actually doing anything to it. So you specify it exactly like you would on the actual element, with a space after each class.

 @Html.LabelFor(model => model.ServiceName, htmlAttributes: new { @class = "SerivcesRegistrationLabel control-label col-md-2" })

Every time you use a comma, inside the htmlAttributes, it's expecting a new attribute, not another class again.

Comments

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.