2012-03-15 63 views
1

我是一個新的MVC 3用戶,我正在嘗試開發電子商務網站。 當我使用產品模型創建產品列表時,我無法放入登錄部分頁面,因爲我在logOnPartial頁面中也使用了Customer模型。ASP.NET MVC 3與登錄部分頁面的產品列表

它有錯誤傳入字典的模型項目類型爲'System.Collections.Generic.List`1 [MvcApplication2.Models.Product]',但是此字典需要一個類型爲'MvcApplication2.Models'的模型項。顧客''。

我明白這是什麼意思。但是,我不知道如何在頁面中顯示帶有產品列表的登錄部分頁面?可能嗎? 我給你一些我的代碼。我該怎麼辦?感謝您的閱讀。

//mac.cshtml page 

@model IEnumerable<MvcApplication2.Models.Product> 

<div id="border_frame"> 
@Html.Partial("_LogOnPartial") 
</div> 

當前,我在logOnPartial頁面中使用Customer模型。

//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" }); 

} 

}

回答

0
@Html.Partial("_LogOnPartial", <You need to pass an instance of Customer here>) 

您正在試圖使這需要作爲模型的Customer實例_LogOnPartial視圖。由於你沒有明確地傳遞任何對象(在Partial上有一個覆蓋需要模型),整個現有模型通過(IEnumerable<MvcApplication2.Models.Product> )。這不是你的局部視圖的有效模型,因此是錯誤。

我建議你爲Mac視圖模型從IEnumerable<Product>更改爲新的視圖模型類

class ProductsViewModel 
{ 
    public IEnumerable<Product> Products { get; set; } 
    public Customer Customer { get; set; } 
} 

,然後您可以將您的客戶傳遞給局部視圖

@Html.Partial("_LogOnPartial", Model.Customer) 
+0

那麼我就不能用戶_Logon產品列表中的部分視圖?我怎麼能通過一個客戶的實例?對不起,我是新的MVC,那麼你能解釋一些更多的做法嗎? – wholee1 2012-03-15 09:15:34