Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(PartialView) The model item passed into the dictionary is of type 'Customer', but this dictionary requires a model item of type 'UserProfile'

@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?

like image 796
Rasmus-E Avatar asked May 10 '13 08:05

Rasmus-E


2 Answers

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?

like image 138
Murray Avatar answered Oct 03 '22 05:10

Murray


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) } 
like image 43
Kcoder Avatar answered Oct 03 '22 05:10

Kcoder