2010-04-26 38 views
0
I am getting this following error , when i am tring to having two user controls in one page. 

傳遞到詞典中的模型項的類型爲 'System.Linq.EnumerableQuery 1[Data.EventLog]' but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1 [Data.Notes]'。ASP.NET MVC在單一的.aspx使用多個用戶控件(視圖)

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
    Test 
</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 

    <h2>Test</h2> 

    <% Html.RenderPartial("~/Views/Shared/UserControl/Tracking.ascx"); %> 
    <% Html.RenderPartial("~/Views/Shared/UserControl/Notes.ascx"); %> 
</asp:Content> 

回答

3

Notes.ascx控制是強類型的,但你是不是在您的通話傳遞模式RenderPartial。因此,頁面的模型被傳遞,這是用戶控件的錯誤類型。您需要在呼叫中傳遞模型RenderPartial

例如,如果事件日誌頁面模型有一個名爲NotesModel屬性:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MyNamespace.EventLogModel>" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
    Test 
</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 

    <h2>Test</h2> 

    <% Html.RenderPartial("~/Views/Shared/UserControl/Tracking.ascx"); %> 
    <% Html.RenderPartial("~/Views/Shared/UserControl/Notes.ascx", Model.NotesModel); %> 
</asp:Content> 

注意我:

  1. 改變了Inherits@Page指令到指定模型類型和,
  2. 添加model參數RenderPartial

您當然需要在您的控制器中填入Model.NotesModel。如果它是null,您會看到您目前看到的相同錯誤。

+0

我其實是新手,請給我一個這樣的例子嗎?謝謝 – Pinu 2010-04-27 14:18:58

+0

現在添加的例子。 – 2010-04-27 15:02:08

+0

謝謝,我欣賞它:-) – Pinu 2010-04-28 14:49:40

相關問題