{
  "id": "dotnet/razor-page-view-data-dictionary",
  "signature": "System.InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'X', but this ViewDataDictionary instance requires a model item of type 'Y'.",
  "signature_zh": "System.InvalidOperationException: 传递给 ViewDataDictionary 的模型项类型为 'X'，但此 ViewDataDictionary 实例要求模型项类型为 'Y'。",
  "regex": "The model item passed into the ViewDataDictionary is of type '([^']+)', but this ViewDataDictionary instance requires a model item of type '([^']+)'",
  "domain": "dotnet",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "ASP.NET Core Razor Page or View expects a strongly-typed model of type Y, but the controller or page handler passes a model of type X, causing a type mismatch in ViewDataDictionary.",
  "root_cause_type": "generic",
  "root_cause_zh": "ASP.NET Core Razor 页面或视图期望类型为 Y 的强类型模型，但控制器或页面处理程序传递了类型为 X 的模型，导致 ViewDataDictionary 中的类型不匹配。",
  "versions": [
    {
      "version": ".NET 6.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": ".NET 7.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": ".NET 8.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "ASP.NET Core 6.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "ASP.NET Core 7.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "ASP.NET Core 8.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Change the @model directive in the view to match the passed type X",
      "why_fails": "This breaks the view's contract with its expected model type; the view may use properties of Y that don't exist on X, causing runtime errors.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "Use ViewBag or ViewData to pass the model instead of a strongly-typed model",
      "why_fails": "This defeats the purpose of strongly-typed views and can lead to runtime casting errors and loss of IntelliSense.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "Cast the model to type Y in the controller",
      "why_fails": "If X and Y are unrelated types, casting will throw InvalidCastException at runtime.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Ensure the controller action returns a ViewResult with the correct model type: return View(modelOfTypeY); where modelOfTypeY is of the type declared in the view's @model directive.",
      "success_rate": 0.95,
      "how": "Ensure the controller action returns a ViewResult with the correct model type: return View(modelOfTypeY); where modelOfTypeY is of the type declared in the view's @model directive.",
      "condition": "",
      "sources": []
    },
    {
      "action": "If the view must accept multiple types, change the @model directive to a base class or interface that both X and Y implement, then adjust the view accordingly.",
      "success_rate": 0.85,
      "how": "If the view must accept multiple types, change the @model directive to a base class or interface that both X and Y implement, then adjust the view accordingly.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use a ViewModel that contains both X and Y as properties, and pass that ViewModel to the view.",
      "success_rate": 0.9,
      "how": "Use a ViewModel that contains both X and Y as properties, and pass that ViewModel to the view.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "确保控制器操作返回正确模型类型的 ViewResult：return View(modelOfTypeY); 其中 modelOfTypeY 的类型与视图的 @model 指令声明的类型一致。",
    "如果视图必须接受多种类型，将 @model 指令改为 X 和 Y 都实现的基类或接口，然后相应调整视图。",
    "使用包含 X 和 Y 作为属性的 ViewModel，并将该 ViewModel 传递给视图。"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://learn.microsoft.com/en-us/aspnet/core/mvc/views/overview",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.95,
  "resolvable": "true",
  "first_seen": "2023-03-10",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}