Blazor: Conditionally rendered attributes are ignored.
See original GitHub issueOn 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:
- Created 4 years ago
- Comments:5 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Hi @rxelizondo the way to do what you’re asking is:
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.
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