dotnet / dotnet/aspnetcore

Apply tag helper attributes conditionally

Open
#44,151 1 comment 0 reactions 0 assignees View on GitHub
area-mvc enhancement
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

Attempting to conditionally apply different pass through attributes on a tag helper. As far as I can tell this is only possible with [boolean attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes#boolean_attributes) (required, readonly), and the only way around this is conditionally displaying an entirely different tag helper with the attributes you want?

Related documentation: https://learn.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/intro?view=aspnetcore-6.0#c-in-tag-helpers-attributedeclaration

Example usage:
```html

<-- generates something like... -->

```

`disabled` is removed but an attribute like `data-example` remains.

Vue 3 for example removes attributes that have a value of null. But it appears tag helpers convert null to empty strings.

Current behavior:
```html

<-- generates something like... -->

```

Ideal behavior:
```html

<-- could generate something like... -->

```

Is there a way to achieve this or is this a potential feature?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.