0

Consider the following Razor component.

@code
{
    private bool isIndex = true;
}


@if (isIndex)
{
    <NavLink href="" Match=NavLinkMatch.All>
        Index
    </NavLink>
}
else
{
    <NavLink href="Other">
        Other
    </NavLink>
}

Is it possible to use a conditional construct to enable Match=NavLinkMatch.All that renders the same output as above?

<NavLink href=@(isIndex? string.Empty:"Other")>
    @(isIndex? "Index": "Other")
</NavLink>
Second Person Shooter
  • 14,188
  • 21
  • 90
  • 165

1 Answers1

2

Is it possible to use a conditional construct to enable Match=NavLinkMatch.All

It is an enum with two values.

You can just use Match="@(IsIndex ? NavLinkMatch.All : NavLink.Prefix)"
Prefix is the default so you don't see it much.

But more in general: no, you can only apply C# logic to the values of attributes. Unless you want to drop down to BuildRenderTree code.

H H
  • 263,252
  • 30
  • 330
  • 514
  • I guess NavLink is just an example. In my opinion OP is not looking for a solution for NavLink but a generic solution for any case. For example, do you want to set `MaxLeng` only if a flag is true. [By default MacLebg is Unlimited](https://www.w3.org/TR/html401/interact/forms.html) – dani herrera Jul 01 '20 at 19:43
  • Yes, I picked up on the specific case in the question. I just quoted it. – H H Jul 01 '20 at 20:35
  • Now is fine for me. It's a pity this is a not supported feature. – dani herrera Jul 01 '20 at 20:49
  • Yes, something like how disabled=false works. But it would be hard to come up with something that would work for all attributes. – H H Jul 01 '20 at 21:03
  • Henk, what about splatting attributes: https://stackoverflow.com/a/58022065/842935 – dani herrera Jul 02 '20 at 20:43
  • Yes, with splatting you could make something work for `` but I don't see a general solution. – H H Jul 03 '20 at 05:39