You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
Add a property to be accessed from the page's CSHTML file: public string Message { get; set; } = "Hello";
Open the Index.cshtml page file
Attempt to render the value of Model.Message inside the "Scripts" section:
@section Scripts {
<h1>@Model.Message</h1>
}
Expected Result
The message is rendered in the HTML OK.
Actual Result
InvalidCastException: Unable to cast object of type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary`1[System.Object]' to type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary`1[WebApplication26.Pages.IndexModel]'.
Razor generates a property like the following. Code here
public ViewDataDictionary<TModel> ViewData => (ViewDataDictionary<TModel>)PageContext?.ViewData;
It's this cast operation that's blowing up.
When we're executing the Layout, we'll set ViewContext.ViewDatahere to the appropriate type for the Layout page, which will be ViewDataDictionary<object>.
I'm not sure why it's the same ViewContext. We should be providing a different ViewContext to the Layout.
Repro
Index.cshtml.cs
model class filepublic string Message { get; set; } = "Hello";
Index.cshtml
page fileModel.Message
inside the "Scripts" section:Expected Result
The message is rendered in the HTML OK.
Actual Result
@rynowak @NTaylorMullen
The text was updated successfully, but these errors were encountered: