How to insert a new line in a [DisplayName] annotation
Is there anyway of putting a new line in a [DisplayName()]
annotation of an mvc viewmodel? I currently have a property that is:
[DisplayName("Delivery Time (if different from our normal delivery of 10-30am – 12.30pm):")]
public s开发者_运维知识库tring DeliveryTime { get; set; }
and I want to have a new line after the Delivery Time part so that the bit in brackets is below, is that possible? I've tried \r\n
but that didn't work.
Well, the problem you have is that \n
is ignored in html. You'd need to use <br />
BUT that does tie your attribute metadata to using html formatting.
I'm looking for a prettier solutions, but a work around is presented in How to add a new line into Display Attribute, Name field
To summarize you put a <br> (or <br/>) in your DisplayName string and use
@Html.Raw(ViewData.ModelMetadata.DisplayName)
to display it.
You can just use style="white-space: pre-line"
in your element and \n
in DisplayName
attribute. At least, it worked for me.
精彩评论