Home > Blockchain >  razor syntax insert C# string value into tags section
razor syntax insert C# string value into tags section

Time:10-26

I use razor file and want use C# btnStateTag variable as tag name. So I use it as @btnStateAttr in HTML part. But it doesn't work

@{
    string btnStateAttr = "";
  if (EditFormParameter.SendingCompleted)
  {
      btnStateAttr = "disabled";
  }
}

  <div >
    <button  @btnStateAttr type="button">Send</button>
  </div>

What is wrong here.

I want to add an attribute - disabled.

CodePudding user response:

You can do it like this:

<button  disabled="@EditFormParameter.SendingCompleted" type="button">Send</button>

No need for the extra variable.

When condition is true razor will generate:

<button  disabled="disabled" type="button">Send</button>

and when condition is false it will generate:

<button  type="button">Send</button>

CodePudding user response:

Maybe you can try to use @if:

    <div >
    @if(btnStateAttr == "disabled")
    {
        <button  disabled type="button">Send</button>
    }else
    {
        <button  type="button">Send</button>
    }
        
  </div>
  • Related