The model item passed into the dictionary is of ty

2019-01-08 12:22发布

I have this annoying mistake in some of my builds.

There is no error in the project, because if I build again, then the problem disappears. The message only appears, when the site is deployed to a Windows 2008 Server.

I first thought that it might be an issue with temporary files, but thats not the case. I deployed the build to a different web and the error still appears.

The error appears on random actions of the site. Most of the time builds are ok, but each 3rd or 4th build produces runtime errors.

I build using a WebdeploymentProject in release mode. Views are precompiled.

It's not In ASP.NET MVC I encounter an incorrect type error when rendering a page with the correct typed object, because views have totally different names.

How I can debug this problem or how I can get help for this?

Here is my WebDeploymentProject

    <!-- 
      Microsoft Visual Studio 2008 Web Deployment Project 
      http://go.microsoft.com/fwlink/?LinkID=104956

    -->
    <Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
        <ProductVersion>9.0.21022</ProductVersion>
        <SchemaVersion>2.0</SchemaVersion>
        <ProjectGuid>{E5E14CEB-0BCD-4203-9A5A-34ABA9C717EA}</ProjectGuid>
        <SourceWebPhysicalPath>..\B2CWeb</SourceWebPhysicalPath>
        <SourceWebProject>{3E632DB6-6DB3-4BD0-8CCA-12DE67165B48}|B2CWeb\B2CWeb.csproj</SourceWebProject>
        <SourceWebVirtualPath>/B2CWeb.csproj</SourceWebVirtualPath>
        <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
      </PropertyGroup>
      <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
        <DebugSymbols>true</DebugSymbols>
        <OutputPath>.\Debug</OutputPath>
        <EnableUpdateable>false</EnableUpdateable>
        <UseMerge>true</UseMerge>
        <SingleAssemblyName>B2CWeb_Build</SingleAssemblyName>
      </PropertyGroup>
      <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
        <DebugSymbols>false</DebugSymbols>
        <OutputPath>..\B2CWeb_Deploy\</OutputPath>
        <EnableUpdateable>false</EnableUpdateable>
        <UseMerge>true</UseMerge>
        <SingleAssemblyName>B2C_Web</SingleAssemblyName>
        <ContentAssemblyName>
        </ContentAssemblyName>
        <DeleteAppCodeCompiledFiles>false</DeleteAppCodeCompiledFiles>
      </PropertyGroup>
      <ItemGroup>
      </ItemGroup>
      <Import Project="$(MSBuildExtensionsPath)\Microsoft\WebDeployment\v9.0\Microsoft.WebDeployment.targets" />
      <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
           Other similar extension points exist, see Microsoft.WebDeployment.targets.
      <Target Name="BeforeBuild">
      </Target>
      <Target Name="BeforeMerge">
      </Target>
      <Target Name="AfterMerge">
      </Target>
      <Target Name="AfterBuild">
      </Target>
      -->
    </Project>

EDIT

After some months this problem disapeared. I haven't had problems for over 1 year now. I guess the issue will strike again when nobody expects it.

EDIT 2

… for over 2 years now. I am such a lucky dude!

EDIT 3

I just read an article on MSDN, and it sounds like the problem I had. I have debugging disabled, but still the compilation was "sometimes wrong". The behaviour of the old provider could be the issue. But this is just guessing.

Very large pages containing long spans of HTML with no server blocks (e.g. <%= %>) can cause a stack overflow when debug compilation is enabled that will crash the application. Note that in our testing it’s taken a page that’s ~4x as large one that would’ve produced a similar issue in the old provider, but in this case it crashes the entire application, whereas with the old provider it just fails the offending page

9条回答
女痞
2楼-- · 2019-01-08 12:41

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?

查看更多
趁早两清
3楼-- · 2019-01-08 12:43

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

查看更多
叼着烟拽天下
4楼-- · 2019-01-08 12:48

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...

查看更多
登录 后发表回答