Solve the "Model item passed to the dictionary" error in the ASP.NET MVC view
When encountering an error prompt, "The model type passed to the dictionary is BAR, but this dictionary requires the type of FOO", it means that the expected model type in the view does not match the model type that is transmitted.
Reason 1: The controller transmits the wrong model to the view
The most common reason is that the controller conveys the wrong model to the view. This happens when the model type (@model foo) is not matched with the model type (@model foo) in the view.
To solve this problem, please explicitly convert the model into the correct type in the controller:
Reason 2: The model of the view is transmitted to some views to some views
var model = (Foo)db.Foos.Select(x => new { ID = x.ID, Name = x.Name });
When using some views, the default model is the model of the main view. If some views are expected to be different models, please explicitly specify it:
<因> Reason 3: The model statement in the layout
@Html.Partial("_Bar", Model.MyBar)
Remember, this error usually does not match the expected model type in the view. You can effectively solve this error by carefully checking the code and finding the source of not matching.
The above is the detailed content of Why Does My ASP.NET MVC View Throw a 'Model Item Passed into the Dictionary' Error?. For more information, please follow other related articles on the PHP Chinese website!