I am developing an ASP.Net MVC 3 web application. I have a ViewModel
public class ViewModelFormPreview
{
public FormPersonal PersonDetails { get; set; }
public IList<FormEmployment> Employment { get; set; }
public FormMembership Membership { get; set; }
}
And a Razor View which accepts this ViewModel
@model Locum.UI.ViewModels.ViewModelFormPreview
<h1 class="sepH_b">Application Preview</h1>
@Html.Partial("FormPreviewApplication/PersonalDetails", Model.PersonDetails)
@Html.Partial("FormPreviewApplication/Employment", Model.Employment)
@Html.Partial("FormPreviewApplication/MembershipDetails", Model.Membership)
The last part of the View is where my problem is happening.
If Model.Memebership != null then the Partial View, which accepts an Object of type FormMembership, works nicely. However, if Model.Memebership is NULL, I get an error like below
The model item passed into the dictionary is of type
‘Locum.UI.ViewModels.ViewModelFormPreview’, but this dictionary
requires a model item of type ‘Locum.POCO.FormMembership
This is a strange error, because, even if Model.Memebership is NULL, it is still of type FormMembership. So then, why is the error stating I am trying to pass in ViewModelFormPreview when the Object is NULL?
Can anyone help with this?
Thanks.
As the message says, it is NULL and not EMPTY.
If you try to use Membership.MyFiels, with membership null, you always get an error.
To resolve your problem simply initialize your class, with default values in case of NULL, or use an IF to check if it’s NULL or not
Something like this.