Reputation: 842
@model Customer
@Html.Partial("_UserProfile", (UserProfile)Model.UserProfile)
When i run this code, i get this error:
The model item passed into the dictionary is of type 'Customer', but this dictionary requires a model item of type 'UserProfile'.
Partial View _UserProfile is strongly typed.
I want to be able to edit these field. Any suggestions?
Upvotes: 47
Views: 20889
Reputation: 2008
Make sure your Model.UserProfile
is not null.
I found your post trying to debug the same error, and it turned out I hadn't initialised my "Model.UserProfile
" equivalent.
I guess what's happening here, is that if a null model is passed to RenderPartial
, it defaults to using the main view's model? Can anyone confirm this?
Upvotes: 109
Reputation: 122
I have faced the same problem but finally I had figured it out.
There is a type mismatch in passed models .. Your View accepts model of type Customer
but you partial view is passing the model Userprofile
so what you have to do is pass the same model in both or.... create a model that have all properties of both models. Surely your problem will be solved.
Upvotes: 2
Reputation: 101
I ran into this problem when dealing with parts of a user profile such as Name and Address records. If the user had an incomplete profile I want the account management view to detect a null Address record and display an Action link to create a new Address or display whatever address data is available.
As described by others when null is passed the overload for Html.RenderPartial gets triggered and the parent View Model is passed through. I ended up converting my partial views to Display and Editor Templates to get around it. Here are some How-To articles from: Hansleman and codeguru
You get better re-usability from this method and it preserves the null values: In your View:
@Html.DisplayFor( m=> m.Address)
Then handle the null value in the DisplayTemplate.
@model Namespace.Models.MyObject
...
if(@Model != null){
...
}else{
...
}
Upvotes: 1
Reputation: 1634
It will fallback on initial model if passed item is null.
Try this:
@Html.Partial("_UserProfile", (UserProfile)Model.UserProfile ?? new UserProfile())
Upvotes: 0
Reputation: 3480
If Model.UserProfile is null, it will attempt to pass in your customer model.
Two ways to get around this:
@model Customer
@Html.Partial("_UserProfile", (UserProfile)Model.UserProfile, new ViewDataDictionary())
Or:
@model Customer
if (Model.UserProfile != null)
{
@Html.Partial("_UserProfile", (UserProfile)Model.UserProfile)
}
Upvotes: 23
Reputation: 51
Add the keyword "virtual" to the UserProfile property on the Customer model. It is the easyest way overcome the lazy loading, but performance..
Upvotes: -1
Reputation: 10208
Your trying to case a Customer
type object to a UserProfile
type object. By default this wont work as the framework has no idea how to cast these objects. If you absolutely must do it this way the only option is to provide explicit cast operator like:
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;
}
You can read more about it here.
Upvotes: -1