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:30

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.

查看更多
Ridiculous、
3楼-- · 2019-01-08 12:31

ASP.NET MVC Partials

NULL model was passed!!!

@Html.Partial("PartialView", model: Model)
查看更多
聊天终结者
4楼-- · 2019-01-08 12:34

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

查看更多
在下西门庆
5楼-- · 2019-01-08 12:36

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);
查看更多
爷的心禁止访问
6楼-- · 2019-01-08 12:38

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?

查看更多
▲ chillily
7楼-- · 2019-01-08 12:41

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

查看更多
登录 后发表回答