Razor Adds New Attribute To HTML

I want to add a new attribute to my HTML dynamically, but I cannot figure out how to do this:

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

I tried a couple of things, but now my code looks like this:

<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?

+4
source share
1 answer

I believe you want to use the <text> , 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> 
+5
source

Source: https://habr.com/ru/post/1379711/


All Articles