I am using Razor pages in .net 5.0
I wanted to use anchor tag helper to generate href
as shown below
<a asp-page="/Add" class="ml-4 d-lg-block" @* something *@)"
When add any c# code with tag helper it is giving a compile error
in this I have added @* something *@
but it is giving a compile error
and also If I use any condition to render a tag, It is throwing compile error
<a asp-page="/Edit" @(Model.Condition?"":"disabled")>
saying compile error : The taghelper ‘a’ must not have c# in the elemnts attribute declaration area
How can I use C# along with tag helpers to satisfy both of the above condtions
2
Answers
This is the syntax you have to use to conditionally add the
disabled
attribute:When condition is true it will generate:
and when condition is false it will generate:
Keep in mind that anchor elements do not have a
disabled
attribute so you have to add additional CSS to disable them:This solution is explained in more detail here: https://stackoverflow.com/a/10276157/10839134
disabled
will not work for a tag,you can try to use a button,and addwindow.location.href
in onclick: