SteinTech
SteinTech

Reputation: 4068

ASP.NET Core Model Binding - InvalidOperationException when defining different @model's in _Layout and View

I get an InvalidOperationException when defining different @model's for _Layout and a different view.

InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'ConsumeWebAPI_Basic.ViewModels.ApiResponse', but this ViewDataDictionary instance requires a model item of type 'ConsumeWebAPI_Basic.ViewModels.MovieSearch'.

_Layout.cshtml

@model ConsumeWebAPI_Basic.ViewModels.MovieSearch

Response.cshtml

@model ConsumeWebAPI_Basic.ViewModels.ApiResponse

Upvotes: 0

Views: 295

Answers (2)

SteinTech
SteinTech

Reputation: 4068

I ended up using Partial View and passes the model through @Html.Partial

@Html.Partial("_Menu", new ConsumeWebAPI_Basic.ViewModels.MovieSearch(), new ViewDataDictionary(this.ViewData))

Upvotes: 0

J. Doe
J. Doe

Reputation: 2747

So remove model from _Layout.cshtml or use different layout with the same model cuz .Net cant convert byself MovieSearch to ApiResponse. The model between layout and view is shared

At least you can use dynamic model in layout but i can't recommend this

Upvotes: 2

Related Questions