I am a new MVC 3 user, and I am trying to develop E-Commerce website.
When I created product list by using product model, I cannot put login partial page because I am also using Customer model in logOnPartial page.
It has errors ‘The model item passed into the dictionary is of type ‘System.Collections.Generic.List`1[MvcApplication2.Models.Product]’, but this dictionary requires a model item of type ‘MvcApplication2.Models.Customer”.
I understand what does it mean. However, I don’t know how to display login partial page with product list in a page? Is it possible?
I give you some my code. What shall I do? Thanks you for reading.
//mac.cshtml page
@model IEnumerable<MvcApplication2.Models.Product>
<div id="border_frame">
@Html.Partial("_LogOnPartial")
</div>
Currently, I am using Customer models in logOnPartial page.
//logOnPartial.cshtml page
@model MvcApplication2.Models.Customer
@if (Request.IsAuthenticated)
{
<text>Welcome <strong>@User.Identity.Name</strong>!
[ @Html.ActionLink("Log Off", "LogOff", "Account") ]</text><br />
}
else
{
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
using (Html.BeginForm())
{
<div>
@Html.LabelFor(m => m.userName)
@Html.TextBoxFor(m => m.userName, new { style = "width:150px;" })
@Html.ValidationMessageFor(m => m.userName)
@Html.LabelFor(m => m.password)
@Html.PasswordFor(m => m.password, new { style = "width:150px;" })
@Html.ValidationMessageFor(m => m.password)
@Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.")
</div>
<input type="submit" class="login_button" value="Log in"/>
@Html.ActionLink("Register now", "Register", "Account", null, new { @class = "register_button" });
}
}
You are trying to render _LogOnPartial view which requires as a model an instance of
Customer. Since you’re not passing any object explicitly (there’s an override onPartialthat takes a model) the whole existing model is passed (IEnumerable<MvcApplication2.Models.Product>
). This is not a valid model for your partial view, hence the error.I would suggest that you change the model for mac view from
IEnumerable<Product>to a new view model classand then you can pass your customer to the partial view