Leon Cullens
Leon Cullens

Reputation: 12496

Razor add new attribute to HTML

I want to add a new attribute to my HTML dynamically, but I can't figure out how to do this;

I have some Facebook like button code, where I want to add a "data-url" attribute if the Url property in my model is specified.

I've tried a couple of things but this is what my code looks like now:

<a href="https://twitter.com/share" class="twitter-share-button" @if (!string.IsNullOrEmpty(Model.Url)) { data-url="@Model.Url" } data-text="@Model.TweetText" data-count="vertical" data-via="avalaxy">Tweet</a><script type="text/javascript" src="//platform.twitter.com/widgets.js"></script>

This does not work. So how do I do this?

Upvotes: 4

Views: 3414

Answers (1)

Heretic Monkey
Heretic Monkey

Reputation: 12124

I believe you want to use the <text> tag, with something like:

<a href="https://twitter.com/share" class="twitter-share-button" @if (!string.IsNullOrEmpty(Model.Url)) { <text>data-url="@Model.Url"</text> } data-text="@Model.TweetText" data-count="vertical" data-via="avalaxy">Tweet</a><script type="text/javascript" src="//platform.twitter.com/widgets.js"></script>

Upvotes: 7

Related Questions