Assassinbeast
Assassinbeast

Reputation: 1277

How to set code in attributes if its a taghelper?

if i make a select & option tags, then visual studio will render it purple, marking it a taghelper.

But then i cant put in custom attributes with code like this:

enter image description here

Its red, but the @ should be yellow, and the foo should be normally black.

When i run it, the first option should have the attribute called data-foo, but it doesn't exist.

But when put code inside a script tag that is drawed purple, then it works, like this:

enter image description here

Here, it has the normal color, and when i run it and look in developer tools, i can see the data-foo attribute in the dom.

However, if i put the @foo code at the end, then it doesn't work, similar to option, like this:

enter image description here

So, is this a bug or what is going on?

I also tried to put to @foo at the beginning of the option tag, but it still draws it red.

Upvotes: 2

Views: 129

Answers (1)

RonC
RonC

Reputation: 33851

I don't believe it's currently possible to use @ syntax to specify an attribute name in a tag helper. However, you can use @ syntax to specify an attribute value like so:

@{
     string fooValue = "someValue";
 }


 <select>
    <option value="red" data-foo="@fooValue">Red</option>
    <option value="green">Green</option>
 </select>

and it color codes accordingly:

enter image description here

Upvotes: 1

Related Questions