首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >传入字典的模型项的类型为“mvc.Models.ModelA”,但此字典需要类型为“mvc.Models.ModelB”的模型项

传入字典的模型项的类型为“mvc.Models.ModelA”,但此字典需要类型为“mvc.Models.ModelB”的模型项
EN

Stack Overflow用户
提问于 2010-02-23 07:39:23
回答 10查看 100K关注 0票数 70

在我的一些构建中,我有这个恼人的错误。

项目中没有错误,因为如果我再次构建,那么问题就会消失。只有在将站点部署到Windows 2008 Server上时,才会显示该消息。

我最初认为这可能是临时文件的问题,但事实并非如此。我将构建部署到不同的网站,但错误仍然出现。

该错误出现在网站的随机操作中。大多数时候构建是正常的,但是每个第三或第四个构建都会产生运行时错误。

我在发布模式下使用WebdeploymentProject进行构建。视图是预编译的。

它不是In ASP.NET MVC I encounter an incorrect type error when rendering a page with the correct typed object,因为视图有完全不同的名称。

如何调试此问题或如何获得相关帮助?

这是我的WebDeploymentProject

代码语言:javascript
复制
    <!-- 
      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>

编辑

几个月后,这个问题就消失了。我已经一年多没有遇到问题了。我猜这个问题会在没有人预料到的时候再次出现。

编辑2个

…已经两年多了。我真是个幸运的家伙!

编辑3个

我刚刚读了an article on MSDN,这听起来像是我遇到的问题。我禁用了调试,但编译仍然“有时出错”。旧供应商的行为可能是问题所在。但这只是猜测。

启用调试编译时,如果

非常大的页面包含没有服务器块的长跨度超文本标记语言(如<%= %>),则可能导致堆栈溢出,从而使应用程序崩溃。请注意,在我们的测试中,它获取的页面大约是旧提供程序的4倍,这在旧的提供程序中会产生类似的问题,但在这种情况下,它会使整个应用程序崩溃,而对于旧的提供程序,它只是导致有问题的页面

失败

EN

回答 10

Stack Overflow用户

发布于 2010-03-20 02:55:44

即使类型匹配,当将null传递给分部视图时,也可能会出现此错误。

您可以通过使用如下所示的空ViewDataDictionary调用RenderPartial来修复此问题:

代码语言:javascript
复制
helper.RenderPartial("~/Views/Player/PlayerName.ascx", player, new ViewDataDictionary());

作为参考,我在以下位置找到了此解决方案:

renderpartial with null model gets passed the wrong type

票数 175
EN

Stack Overflow用户

发布于 2013-11-26 03:21:16

ASP.NET MVC分区

传递的模型为空!

代码语言:javascript
复制
@Html.Partial("PartialView", model: Model)
票数 14
EN

Stack Overflow用户

发布于 2010-03-07 03:46:43

当控制器操作方法提供给视图的数据与视图期望的数据类型不匹配时,就会出现此错误。这通常不会显示为构建错误,即使使用预编译的视图也是如此。

例如,如果我有一个方法...

代码语言:javascript
复制
public ActionResult Create()
{
    // Do something
    return View(new CustomerCreateViewModel());
}

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

代码语言:javascript
复制
<%@ Page ... Inherits="System.Web.Mvc.ViewPage<CustomerDetailsViewModel>" %>

...this将不会出现任何错误地编译和构建。但是,当我调用Create操作时,我将看到一个黄屏,因为Create操作方法抛出的是一种类型的数据,而视图期望的是另一种类型的数据。您可能需要验证您的类型是否匹配...

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2315129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档