Home > OS >  Taghelper attributes must be well formed
Taghelper attributes must be well formed

Time:10-11

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"  @* 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

CodePudding user response:

This is the syntax you have to use to conditionally add the disabled attribute:

<a asp-page="/Edit" disabled="@Model.Condition">...</a>

When condition is true it will generate:

<a href="..." disabled="disabled">...</a>

and when condition is false it will generate:

<a href="...">...</a>

CodePudding user response:

disabled will not work for a tag,you can try to use a button,and add window.location.href in onclick:

@if(Model.Condition==""){
    <button onclick="window.location.href='/Edit'" >...</button>
 }else
{
    <button onclick="window.location.href='/Edit'" disabled">...</button>                
}
  • Related