Skip to content

Commit 99d6b83

Browse files
authored
Clarify Razor's handling of data- attributes (dotnet#35583)
1 parent 852dd54 commit 99d6b83

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

aspnetcore/mvc/views/razor.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,9 @@ Extra `@` characters in a Razor file can cause compiler errors at statements lat
261261

262262
### Conditional attribute rendering
263263

264-
Razor automatically omits attributes that aren't needed. If the value passed in is `null` or `false`, the attribute isn't rendered.
264+
Razor automatically omits attributes that aren't required. If the value passed in is `null` or `false`, the attribute isn't rendered.
265265

266-
For example, consider the following razor:
266+
For example, consider the following Razor markup:
267267

268268
```cshtml
269269
<div class="@false">False</div>
@@ -289,6 +289,20 @@ The preceding Razor markup generates the following HTML:
289289
<input type="checkbox" name="null">
290290
```
291291

292+
Razor retains `data-` attributes if their values are `null` or `false`.
293+
294+
Consider the following Razor markup:
295+
296+
```cshtml
297+
<div data-id="@null" data-active="@false"></div>
298+
```
299+
300+
The preceding Razor markup generates the following HTML:
301+
302+
```html
303+
<div data-id="" data-active="False"></div>
304+
```
305+
292306
## Control structures
293307

294308
Control structures are an extension of code blocks. All aspects of code blocks (transitioning to markup, inline C#) also apply to the following structures:

0 commit comments

Comments
 (0)