dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.48k stars 10.04k forks source link

DisplayAttribute on nullable variables is lost and ModelMetadata is set to default for variable type #18031

Open Sausbolle opened 4 years ago

Sausbolle commented 4 years ago

Describe the bug

When using DisplayAttribute on nullable variables, the ViewData.ModelMetadata is set to default model data for the generic variable type. This leads to any data set to ViewData.ModelMetadata.Name or ViewData.ModelMetadata.Description using DisplayAttribute is nulled. Sorry if bad description (not sure all the technical terms to properly describe it)

To Reproduce

A model like this will in @Html.EditorFor(m => m) (and the like) loose the ViewData.ModelMetadata.Name and ViewData.ModelMetadata.Description for the nullable Int32 (in this example)

    public class BugTestModel
    {
        [Display(Name = "This name is kept in ViewData.ModelMetadata", Description = "This description is kept in ViewData.ModelMetadata")]
        public Int32 TestIntOne { get; set; }

        [Display(Name = "This name is nulled in ViewData.ModelMetadata", Description = "This description is nulled in ViewData.ModelMetadata")]
        public Int32? TestIntNull { get; set; }
    }

Further technical details

Runtime Environment: OS Name: Windows OS Version: 10.0.18363 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\3.1.100\

Host (useful for support): Version: 3.1.0 Commit: 65f04fb6db

.NET Core SDKs installed: 3.1.100 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed: Microsoft.AspNetCore.All 2.1.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

Microsoft.WindowsDesktop.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]



- The IDE (VS / VS Code/ VS4Mac) you're running on, and it's version
`Microsoft Visual Studio Community 2019 Version 16.4.2`

- Test project (showing error)
[BugTest.zip](https://github.com/aspnet/AspNetCore/files/4000947/BugTest.zip)
pranavkm commented 4 years ago

Thanks @Sausbolle for the detailed repro application. This looks like a bug in MVC's editor template \ display template system. While MVC falls back to using the backing type for nullable types, it ends up reconstructing the ModelMetadata to fit the non-null model type. Essentially, it sees a model metadata instance for a Nullable<int> but has to produce a model metadata for an int model. The transformation erases the metadata.

One workaround you could apply is to specify a template to use for nullable types and point to a template that has a nullable model. For e.g.:

// Model.cs
[Display(Name = "This name is nulled in ViewData.ModelMetadata", Description = "This description is nulled in ViewData.ModelMetadata")]
[UIHint("NullableInt32")]
public Int32? TestIntNull { get; set; }
// NullableInt32.cshtml
@model Int32?

...

Parking this in the backlog so we can track fixing this in a later release.

Neme12 commented 3 years ago

I hit this too.