Suppose I have a partial view called UserDetails
whose @model
clause is set to a model class called User
.
Now suppose I have another model class that looks something like this:
public sealed class SpecialModel
{
public User SpecialUser;
public ... // other stuff
}
Inside a view for the SpecialModel
, I want to invoke my partial view mentioned above:
@model MyProject.Models.SpecialModel
@{ ViewBag.Title = "..."; }
<div class='user'>@Html.Partial("UserDetails", Model.SpecialUser)</div>
This works just fine if the user is not null
. However, if the user is null
, I get this exception:
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'.
Clearly, the exception message is lying. How do I fix this properly so that I can pass null
normally?