The model item passed into the dictionary is of type ‘mvc.Models.ModelA’ but this dictionary requires a model item of type ‘mvc.Models.ModelB‘

人盡茶涼 提交于 2019-11-27 06:56:46
Maslow

Even if the types match, you can get this error when a null is passed to a partial view.

You can fix this by calling RenderPartial with an empty ViewDataDictionary like this:

helper.RenderPartial("~/Views/Player/PlayerName.ascx", player, new ViewDataDictionary());

For reference, I found this solution at:
renderpartial with null model gets passed the wrong type

ASP.NET MVC Partials

NULL model was passed!!!

@Html.Partial("PartialView", model: Model)

This error can occur (and does) when there is a mismatch between the data that the controller action method is supplying to the view and the type of data the view is expecting. This will not normally show up as a build error, even with precompiled views.

For example, if I have a method...

public ActionResult Create()
{
    // Do something
    return View(new CustomerCreateViewModel());
}

...and a Create view with a Page attribute...

<%@ Page ... Inherits="System.Web.Mvc.ViewPage<CustomerDetailsViewModel>" %>

...this will compile and build without error. However, when I call the Create action, I'm going to get a yellow screen, because the Create action method is throwing data of one type and the view is expecting data of a different type. You might want to verify that your types are matching...

Are you absolutely sure that this has nothing to do with the data being passed to the view? Are you performing a full rebuild each time?

These errors typically occur because a partial view tries to use the view model passed to the ViewPage when the view model passed to the partial view is null. I realize that you are implying that the error is somehow caused by the build process, but I don't see how that would be possible. Could it be that the deployed site uses a different database than the site you run on your development machine, and could the data (or lack of data) in that database be the cause of the problem?

Do the build trigger any warnings or errors at all?

Do you have your dependencies set up correctly? Eg. views depending on models. Build figures out in what order to compile things by looking at the dependencies specified. Eg. if you change a model and the view is compiled before the model you get into trouble...

Do this disappear if you don't compile views?

Also do your views inherit System.Web.Mvc.ViewPage or System.Web.Mvc.ViewPage<T> where T is your model?

Siva Gopal

Before assigning the model to the usercontrol from your view, instantiate that particular object in the constructor of the Model/Entity.

Example:

 public class MainEntity
 {
    public SubEntity AssociatedEntity;

    public MainEntity()
    {
        // This is where the instantiation happen.
        AssociatedEntity = new SubEntity(); 
    }
 }

 public class SubEntity
 {
    public string property1;
 }

Your View Page :

<%@ Page Title="" Language="C#" 
    MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage<MyNamespace.Models.MainEntity>" %>
....
<%Html.RenderPartial("ucMyUserControl",Model.AssociatedEntity);
....

Your User Control:

<%@ Control Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<MyNamespace.Models.SubEntity>" %>
....
<%Html.TextBoxFor(m=>m.Property1);

If you have an overriding Views folder, where multiple partials of the same name exist (one overriding another with the same name), be sure to update the Model type on each view or you might receive this error.

I had the following error message

The model item passed into the dictionary is of type 'System.Linq.Enumerable+WhereSelectListIterator2[MyProject.Models.A,MyProject.Models.B]', but this dictionary requires a model item of type 'System.Collections.Generic.List1[MyProject.Models.B]'.

with the following code

IEnumerable<B> list_B;
...
IEnumerable<A> list_A = Repo.GetListOfType_A();
list_B = list_A.Select(x => new B { Number = x.Number, Name = x.Name });
...
return View(list_B);

and I have no idea what was the type

System.Linq.Enumerable+WhereSelectListIterator2[MyProject.Models.A,MyProject.Models.B]

anyway, the error goes away when I add a .ToList() at the end

IEnumerable<B> list_B;
...
IEnumerable<A>list_A = Repo.GetListOfType_A();
list_B = list_A.Select(x => new B { Number = x.Number, Name = x.Name }).ToList(); // here
...
return View(list_B);

Hope it helps someone else

For me this was caused by a silly cut and paste error in my context. I was getting this error on my Contact index.

    public DbSet<Customer> Customer { get; set; }
    public DbSet<Customer> Contact { get; set; }
    // Note this ^^^^^^^^ should have been Contact

It compiled okay, but gave me the error when trying to render the view.

(Adding this as something else for future people looking for solutions to try.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!