[MVC] Partial views and multiple models

In one project I have the following page:

EXAMPLE 1

<div class="central-form"> <div id="login-1"> <!-- begin login/register buttons --> <a class="button active-button">Sign In</a> <a class="button inactive-button pos">POS Sign Up</a> <a class="button inactive-button store">Store Sign Up</a> <script> $("#login-1 .active-button").click(function () { $(this).parent().slideUp(); $("#login-2").slideDown(); }) $("#login-1 .pos").click(function () { $(this).parent().slideUp(); $("#login-4").slideDown(); }); $("#login-1 .store").click(function () { $(this).parent().slideUp(); $("#login-3").slideDown(); }) </script> </div> <div id="login-2"> <!-- begin login form --> @Html.Partial("_LoginPartial") </div> <div id="login-3"> <!-- begin registration form --> @Html.Partial("_RegisterStorePartial") </div> <div id="login-4"> <!-- begin pos registration form --> @Html.Partial("_RegisterPosPartial") </div> </div> 

All of the partial views are in the same folder as this page and it works perfectly fine. Exactly as I'd expected it would.

On another project I have this:

EXAMPLE 2

<div id="user-home"> <h2>Your Rewards</h2> <div id="rewards-table"> @Html.Partial("_RewardView") </div> <div id="claim-receipt" style="display: none;"> @Html.Partial("_ClaimView") </div> </div> 

Also with the partial views in the same folder as the page yet this doesn't.

Moreover, the partial views in Example 1, all use different models:

_LoginPartial uses Models.User
_RegisterStorePartial uses Models.Store
_RegisterPosPartial uses Models.POSProvider

The same is true for the Example 2 project:

_RewardView uses an IEnumerable of Models.Reward
_ClaimView uses Models.Receipt

I can't get my head around why Example 1 works, but Example 2 gives me the following error:

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Models.Reward]', but this dictionary requires a model item of type Models.Receipt'.

I know as a matter of best practice, this is the wrong way to do it, but as far as I can see, both examples are structured the same way so why doesn't it work in example 2?

by ortund via /r/csharp

Leave a Reply