dotnet / dotnet/aspnetcore

Cant use asp-for tag helpers with nullable model

Open
#40,327 6 comments 3 reactions 0 assignees View on GitHub
area-mvc enhancement Priority:2
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

After https://github.com/dotnet/aspnetcore/issues/37510 we can mark model as nullable like `@model MyModel?`, but then using asp-for tag helpers
``
``
shows "Dereference of a possibly null reference" during view compilation.

When model not marked nullable and real passed model is null - inputs renders correctly:

``
`TestName`

Probably problem is `LabelTagHelper.ModelExpression` and `InputTagHelper.ModelExpression` is not nullable but can really accept null (https://github.com/dotnet/aspnetcore/issues/5680 shows that Microsoft.AspNetCore.Mvc.TagHelpers not annotated)

### Expected Behavior

View compiled without errors and renders correct inputs

### Steps To Reproduce

1. Mark page model as nullable
2. Create input or label asp-for for any model field

### Exceptions (if any)

Dereference of a possibly null reference

### .NET Version

6.0.200

### Anything else?

_No response_

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.