Skip to content

[Bug] Conditional attribute rendering #60864

Open
@lonix1

Description

@lonix1

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I remember being able to render attributes based on a conditional.

Example:

<div data-foo="@(true ? "abc" : null)" data-bar="@(false ? "123" : null)">

Would render:

<div data-foo="abc">

However I've lately found that it always renders this:

<div data-foo="abc" data-bar="">

The docs explain the attribute will be elided if null.

Expected Behavior

The attribute should be elided if null.

Steps To Reproduce

dotnet new razor -o Demo
cd Demo

Edit _Layout.cshtml:

<body data-foo="@(false ? "abc" : null)">
    <div data-bar="@(false ? "123" : null)">

Run

dotnet run

Inspect the source, which will show this:

<body data-foo="">
    <div data-bar=""></div>

Exceptions (if any)

No response

.NET Version

ASP.NET Core Razor Pages on .NET 8

Anything else?

This breaks scripts which expect the attribute to exist or not exist, as the case may be. I was sure the problem was "just me", until I tried that basic repro above. If it's indeed not just me, this is a very very bad bug.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions