Skip to content

Cant use asp-for tag helpers with nullable model #40327

Open
@G1Vh

Description

@G1Vh

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

After #37510 we can mark model as nullable like @model MyModel?, but then using asp-for tag helpers
<input asp-for="Id" type="hidden">
<label asp-for="Name" class="d-flex w-50"></label>
shows "Dereference of a possibly null reference" during view compilation.

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

<input type="hidden" data-val="true" data-val-required="The Id field is required." id="Id" name="Id" value="">
<label class="d-flex w-50" for="Name">TestName</label>

Probably problem is LabelTagHelper.ModelExpression and InputTagHelper.ModelExpression is not nullable but can really accept null (#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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Priority:2Work that is important, but not critical for the releasearea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesenhancementThis issue represents an ask for new feature or an enhancement to an existing one

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions