The ASP.NET MVC model element passed to the dictionary is of type

I get this nasty error in my opinion:

The model element passed to the dictionary is of type ContactWeb.Models.ContactListViewModel, but this dictionary requires a model element of type ContactWebLibrary.Contact.

in this line of code: @{Html.RenderPartial("Form");}

I am using @model ContactWeb.Models.ContactListViewModel at the top of this file.

Here is my view:

  @model ContactWeb.Models.ContactListViewModel <h2>Edit</h2> @{Html.RenderPartial("Form");} @using (Html.BeginForm()) { <fieldset> <legend>Select roles for this user:</legend> <ul> @foreach(var role in Model.AllRoles) { <li><input name="Roles" type="checkbox" value="@role" />@role</li> } </ul> <input type="submit" /> </fieldset> } 
+4
source share
1 answer

The error says that you should pass the correct model into a partial view.
Since you weren't missing anything, MVC uses the default model, which is the view that invoked the partial model.

This will solve the problem:

 @{Html.RenderPartial("Form", new ContactWebLibrary.Contact());} 

This has nothing to do with RenderPartial VS Partial .

+10
source

Source: https://habr.com/ru/post/1385815/


All Articles