Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.Net MVC 3 Razor View Accept Null Object
    text
    copied!<p>I am developing an ASP.Net MVC 3 web application. I have a ViewModel</p> <pre><code>public class ViewModelFormPreview { public FormPersonal PersonDetails { get; set; } public IList&lt;FormEmployment&gt; Employment { get; set; } public FormMembership Membership { get; set; } } </code></pre> <p>And a Razor View which accepts this ViewModel</p> <pre><code>@model Locum.UI.ViewModels.ViewModelFormPreview &lt;h1 class="sepH_b"&gt;Application Preview&lt;/h1&gt; @Html.Partial("FormPreviewApplication/PersonalDetails", Model.PersonDetails) @Html.Partial("FormPreviewApplication/Employment", Model.Employment) @Html.Partial("FormPreviewApplication/MembershipDetails", Model.Membership) </code></pre> <p>The last part of the View is where my problem is happening. </p> <p>If <strong>Model.Memebership != null</strong> then the Partial View, which accepts an Object of type <strong>FormMembership</strong>, works nicely. However, if <strong>Model.Memebership</strong> is NULL, I get an error like below</p> <blockquote> <p>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</p> </blockquote> <p>This is a strange error, because, even if <strong>Model.Memebership</strong> is NULL, it is still of type <strong>FormMembership</strong>. So then, why is the error stating I am trying to pass in <strong>ViewModelFormPreview</strong> when the Object is NULL?</p> <p>Can anyone help with this?</p> <p>Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload