Skip to content

Commit 2bd9de6

Browse files
authored
Avoid declaring RazorPage<T>.Model as nullable by default (#39332) (#39416)
Contributes to #37510
1 parent d9521ac commit 2bd9de6

File tree

5 files changed

+10
-13
lines changed

5 files changed

+10
-13
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
#nullable enable
2+
*REMOVED*Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>.Model.get -> TModel?
3+
Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>.Model.get -> TModel

src/Mvc/Mvc.Razor/src/RazorPageOfT.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@ public abstract class RazorPage<TModel> : RazorPage
1515
/// <summary>
1616
/// Gets the Model property of the <see cref="ViewData"/> property.
1717
/// </summary>
18-
public TModel? Model => ViewData == null ? default(TModel) : ViewData.Model;
18+
public TModel Model => ViewData.Model;
1919

2020
/// <summary>
2121
/// Gets or sets the dictionary for view data.
2222
/// </summary>
2323
[RazorInject]
2424
public ViewDataDictionary<TModel> ViewData { get; set; } = default!;
25-
2625
}
2726
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
#nullable enable
2+
*REMOVED*Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TModel>.Model.get -> TModel?
3+
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TModel>.Model.get -> TModel

src/Mvc/Mvc.ViewFeatures/src/ViewDataDictionaryOfT.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,10 @@ internal ViewDataDictionary(IModelMetadataProvider metadataProvider)
8686
}
8787

8888
/// <inheritdoc />
89-
public new TModel? Model
89+
public new TModel Model
9090
{
91-
get
92-
{
93-
return (base.Model == null) ? default(TModel) : (TModel)base.Model;
94-
}
95-
set
96-
{
97-
base.Model = value;
98-
}
91+
get => (base.Model is null) ? default! : (TModel)base.Model;
92+
set => base.Model = value;
9993
}
10094
}
10195
}

src/ProjectTemplates/Web.ProjectTemplates/content/StarterWeb-CSharp/Views/Shared/Error.cshtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
<h1 class="text-danger">Error.</h1>
77
<h2 class="text-danger">An error occurred while processing your request.</h2>
88

9-
@if (Model?.ShowRequestId ?? false)
9+
@if (Model.ShowRequestId)
1010
{
1111
<p>
12-
<strong>Request ID:</strong> <code>@Model?.RequestId</code>
12+
<strong>Request ID:</strong> <code>@Model.RequestId</code>
1313
</p>
1414
}
1515

0 commit comments

Comments
 (0)