假设我有一个名为UserDetails的局部视图,其@model子句设置为名为User的模型类.
现在假设我有另一个看起来像这样的模型类:
- public sealed class SpecialModel
- {
- public User SpecialUser;
- public ... // other stuff
- }
在SpecialModel的视图中,我想调用上面提到的局部视图:
- @model MyProject.Models.SpecialModel
- @{ ViewBag.Title = "..."; }
- <div class='user'>@Html.Partial("UserDetails",Model.SpecialUser)</div>
如果用户不为null,这可以正常工作.但是,如果用户为null,则会出现以下异常:
System.InvalidOperationException
: The model item passed into the dictionary is of type ‘MyProject.Models.SpecialModel’,but this dictionary requires a model item of type ‘MyProject.Models.User’.
显然,异常消息是谎言.如何正确修复此问题以便我可以正常传递null?