Raz*_*zer 40 c# asp.net-mvc partial-views helper razor
@model Customer
@Html.Partial("_UserProfile", (UserProfile)Model.UserProfile)
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,我收到此错误:
The model item passed into the dictionary is of type 'Customer', but this dictionary requires a model item of type 'UserProfile'.
Run Code Online (Sandbox Code Playgroud)
部分视图_UserProfile是强类型的.
我希望能够编辑这些字段.有什么建议?
Mur*_*ray 92
确保您的Model.UserProfile不为null.
我发现你的帖子试图调试相同的错误,结果我没有初始化我的"Model.UserProfile"等价物.
我想这里发生的事情是,如果将null模型传递给RenderPartial,它默认使用主视图的模型?谁能证实这一点?
Kco*_*der 22
如果Model.UserProfile为null,它将尝试传入您的客户模型.
解决这个问题的两种方法:
@model Customer
@Html.Partial("_UserProfile", (UserProfile)Model.UserProfile, new ViewDataDictionary())
Run Code Online (Sandbox Code Playgroud)
要么:
@model Customer
if (Model.UserProfile != null)
{
@Html.Partial("_UserProfile", (UserProfile)Model.UserProfile)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16966 次 |
| 最近记录: |