question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Blazor: Conditionally rendered attributes are ignored.

See original GitHub issue

On a Blazor page, given the following markup:

<button @(SomeObject == null ? "abc" : "xyz")>Click me</button>

If “SomeObject” is null then the button tag should include the “abc” attribute in it, if “SomeObject” is not null, the button tag should include the “xyz” attribute in it.

What really happens is that no attributes are rendered regardless of the “SomeObject” value.

Having this work correctly is particularly important when rendering “disabled” or “selected” attributes since those are standalone attributes.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
rynowakcommented, May 10, 2019

Hi @rxelizondo the way to do what you’re asking is:

<option selected="@(SomeObject == null)"> Option Value </option>

Attributes will render conditionally if the value is a boolean.

We don’t support putting arbitrary code inside tag headers, and bug is that this isn’t being reported as a compiler error.

0reactions
PlattevilleWScommented, Oct 24, 2019

Apologies in advance if I’m missing something, but I do think this could be very valuable. I’m setting up a specific data attribute that will be constant throughout my application. In my particular case, I’m setting an attribute called data-vis-field equal to it’s API name, so I might do something like:

<div data-vis-field="banner-image">

It would be great if I could make a constant or a variable someplace that would contain the text “data-vis-field”, so that I could just do:

<div @APIField="banner-image">

That way if the data attribute ever changed for some reason, I wouldn’t have to change it everywhere, just in one place. Also I wouldn’t have to worry about misspelling it.

Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Conditional Attributes that is not boolean in blazor
The above code will introduce a bug that the MyComponent is not updated correctly. The reason is the parameters of <MyComponent> received from ......
Read more >
Blazor InputText: conditionally rendering an attribute-blazor
HTML element attributes are conditionally rendered based on the .NET value. If the value is false or null, the attribute isn't rendered.
Read more >
ASP.NET Core Blazor forms and input components
The following Razor component demonstrates typical elements, components, and Razor code to render a webform using an EditForm component.
Read more >
How do you render elements with conditional attributes?
DOM attributes are rendered based on .NET parameters. If the parameter value is true, the attribute is rendered minimized, otherwise it's not.
Read more >
ASP.NET Core Razor components
The Blazor framework processes a component internally as a render tree, which is the combination of a component's DOM and Cascading Style Sheet ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found