Reputation: 3316
Using the latest version of MVC, I want to change the style of an item. From reading this question, I thought I could use this code:
@Html.DisplayNameFor(model => model.Adresse, new { style="width:1000px;" })
But it won't change the style of any element. How can I fix this code?
Upvotes: 1
Views: 5166
Reputation: 12571
I don't think DisplayNameFor
renders any HTML elements so adding html attributes to the control won't work.
I would wrap your control with a span
and add a CSS class, then add that class to your stylesheet where you can write and manage your styles there.
Something like this:
<span class="display-name">@Html.DisplayNameFor(model => model.Adresse)<span>
Then in your CSS stylesheet add your new class there:
.display-name{
display:inline-block;
border: 1px solid #ff0000;
background-color:#eee;
font-size:15px;
/*add more styles ...*/
}
Upvotes: 5
Reputation: 76547
Generally this is accomplished by using the htmlAttributes
property which is just an anonymous object that contains your preferred styles, just like you have used in your example.
The issue here however is that you are currently attempting to use this with the DisplayNameFor
helper as opposed to one that rendered an actual element such as TextBoxFor
, TextAreaFor
or similar.
The DisplayNameFor
helper on it's own will simply render the actual name of the property that you are targeting.
Upvotes: 0
Reputation: 135
Practically the same answer just with some more notes.
use this :
@Html.TextBoxFor(p => p.Publishers[0].pub_name, new { @style="width: 1000px" })
notice the space between the :
and 1000px
. Avoid using semi-colons if you have only one attribute in style.
Upvotes: -1