This code
<%= Html.LabelFor(model => model.Name) %>
produces this
<label for="Name">Name</label>
But I want this
<label for="Name" class="myLabel">Name</label>
How do you do that?
Code explanation Two CSS classes are defined in the <style> element. The class attribute in <label> assigns one class. Repeatedly clicking the button toggles another class, changing the boldness of the <label>.
Html.Label gives you a label for an input whose name matches the specified input text (more specifically, for the model property matching the string expression): // Model public string Test { get; set; } // View @Html. Label("Test") // Output <label for="Test">Test</label>
Html. ActionLink creates a hyperlink on a view page and the user clicks it to navigate to a new URL. It does not link to a view directly, rather it links to a controller's action.
Sadly, in MVC 3 the Html.LabelFor() method has no method signatures that permit a direct class declaration. However, MVC 4 adds 2 overloads that accept an htmlAttributes anonymous object.
As with all HtmlHelpers it's important to remember that the C# compiler sees class
as reserved word.
So if you use the @ before the class attribute it works around the problem, ie:
@Html.LabelFor(model => model.PhysicalPostcode, new { @class= "SmallInput" })
The @ symbol makes the "class" a literal that is passed through.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With