【问题标题】:(PartialView) The model item passed into the dictionary is of type 'Customer', but this dictionary requires a model item of type 'UserProfile'(PartialView) 传入字典的模型项的类型为“客户”,但此字典需要“用户配置文件”类型的模型项
【发布时间】:2013-05-04 21:55:54
【问题描述】:
@model Customer

@Html.Partial("_UserProfile", (UserProfile)Model.UserProfile)

当我运行这段代码时,我得到了这个错误:

The model item passed into the dictionary is of type 'Customer', but this dictionary requires a model item of type 'UserProfile'.

部分视图_UserProfile 是强类型的。

我希望能够编辑这些字段。 有什么建议吗?

【问题讨论】:

标签: c# asp.net-mvc razor partial-views helper


【解决方案1】:

确保您的 Model.UserProfile 不为空。

我发现你的帖子试图调试同样的错误,结果我没有初始化我的“Model.UserProfile”等价物。

我猜这里发生了什么,如果一个空模型被传递给RenderPartial,它会默认使用主视图的模型吗?谁能证实这一点?

【讨论】:

  • 你是对的。这是一个超载的问题。当您尝试在将模型传递为null 时调用@Html.Partial(<view name>,<model>) 时,实现将假定您已调用仅接受视图名称并将当前视图模型传递给局部视图的方法版本。
  • 是的,你是对的!在大多数情况下,您可以在 ViewModel 构造函数中添加 PropertyThatWasNull = new YourObject() 来纠正此问题。使用 null UserProfile 对象可能会更困难。
  • MVC 上的一个讨厌的错误,刚刚也被它击中了,谢谢你的提示。
  • 我在 MVC 3 中为这个问题创建了一些助手:github.com/q42jaap/PartialMagic.Mvc/blob/master/… 代码可能不再工作,但概念应该是可重用的。
【解决方案2】:

如果 Model.UserProfile 为 null,它将尝试传入您的客户模型。

解决此问题的两种方法:

@model Customer

@Html.Partial("_UserProfile", (UserProfile)Model.UserProfile, new ViewDataDictionary())

或者:

@model Customer

if (Model.UserProfile != null)
{
   @Html.Partial("_UserProfile", (UserProfile)Model.UserProfile)
}

【讨论】:

  • 第二个不行,你需要使用防御型演员:@Html.Partial("_UserProfile", Model as UserProfile)
  • @Echilon:你说得对,第二个不起作用,但你的建议也不起作用。答案中的第一个选项确实解决了这个问题。
  • 第二个是为了在模型为空时跳过局部视图。在此示例中,当没有用户配置文件时显示 UserProfile 视图可能没有用,这样可以避免错误。
【解决方案3】:

我在处理部分用户配置文件(例如姓名和地址记录)时遇到了这个问题。如果用户的个人资料不完整,我希望帐户管理视图检测空地址记录并显示操作链接以创建新地址或显示任何可用的地址数据。

正如其他人所描述的,当传递 null 时,会触发 Html.RenderPartial 的重载并传递父视图模型。我最终将我的部分视图转换为显示和编辑器模板以解决它。以下是来自:Hanslemancodeguru 的一些操作方法文章

您可以从该方法中获得更好的可重用性,并保留空值: 在你看来:

@Html.DisplayFor( m=> m.Address)

然后处理 DisplayTemplate 中的 null 值。

@model Namespace.Models.MyObject
...
if(@Model != null){
...
}else{
...
}

【讨论】:

  • 在我们尝试解决我们自己版本的这个问题的所有方法中,这是迄今为止最好的。 ViewData 和 ModelState(包括 ModelState 错误等)都通过 this 正常传递。大多数其他解决方案都会传递一个新的 ViewData 对象,该对象会破坏任何 ModelState。
  • 正是@mikeschuld 为什么你要压缩错误处理并转换一个新对象,而实际上你的模型是空的?只处理应该处理的状态。公平地说,我没有回答如何使用 Html.Partial 的问题,我只是说如果您的对象可能为空,请不要使用它。
【解决方案4】:

我遇到了同样的问题,但最后我想通了。 传递的模型中存在类型不匹配..您的视图接受Customer 类型的模型,但您的部分视图正在传递模型Userprofile,所以您要做的就是在两者中传递相同的模型,或者....创建一个具有两个模型的所有属性的模型。你的问题一定会解决的。

【讨论】:

    【解决方案5】:

    如果传递的项目为空,它将回退到初始模型。

    试试这个:

    @Html.Partial("_UserProfile", (UserProfile)Model.UserProfile ?? new UserProfile())
    

    【讨论】:

      【解决方案6】:

      您试图将 Customer 类型对象转换为 UserProfile 类型对象。默认情况下,这不起作用,因为框架不知道如何转换这些对象。如果您绝对必须这样做,唯一的选择是提供显式转换运算符,例如:

      public static explicit operator Digit(byte b)  // explicit byte to digit conversion operator
      {
          Digit d = new Digit(b);  // explicit conversion
      
          System.Console.WriteLine("Conversion occurred.");
          return d;
      }
      

      您可以阅读更多关于它的信息here

      【讨论】:

      • 属性 UserProfile 有一个演员表。在这种情况下,我认为它应该是不必要的,但它看起来像是明确地试图告诉编译器这是我传入的类型。
      【解决方案7】:

      将关键字“virtual”添加到 Customer 模型的 UserProfile 属性。 这是克服延迟加载的最简单方法,但性能..

      【讨论】:

        猜你喜欢
        • 2020-09-08
        • 2013-10-18
        • 1970-01-01
        • 1970-01-01
        • 2013-10-17
        • 2015-09-27
        • 2014-02-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多