how to set CSS attributes of label in MVC 3
I am creating a static using Html.LabelFor(...). I have to set Name attribute of the label dynamically usi开发者_如何学编程ng JQuery.
You can set the css class, and set inline styles and any other attribute (even non-existant ones like name) using the htmlAttributes parameter provided in one of the overloads of LabelFor
ie
<%: Html.LabelFor(model=>model.Title,
new { style="xyz", @class="abc", @name="MyTitle" }) %>
this would create a label something like:
<label for="Title" style="xyz" class="abc" name="MyTitle">Title</label>
The reason for the @ before class, is that "class" is a reserved word in c#, so you need to qualify it using the @ symbol.
If I understand your question and comments together, you're just trying to change the text of a label. The MVC LabelFor
turns into an HTML <label>
and that doesn't have any attributes like a text box does.
If you need to change it with JS/jQuery then give it an ID and use the html
method.
Markup:
@Html.LabelFor(m => m.Something, new { id = "somethingLabel" })
@Html.TextBoxFor(m => m.Something)
jQuery:
$("#somethingLabel").html("New Label Text");
You could also use text
instead of html
- the difference is just that it escapes the input.
精彩评论