ASP.NET MVC Using Multiple user controls on a single .aspx(view)
I am getting this following error , when i am tring to having two user controls in one page.
The model item passed into the dictionary is of type 'System.Linq.EnumerableQuery1[Data.EventLog]' but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[Data.Notes]'.
<%@ 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>
Your Notes.ascx
control is strongly-typed, but you are not passing a model in your call to RenderPartial
. Hence, the model for the page is passed instead, which is the wrong type for the user control. You need to pass a model in your call to RenderPartial
.
For example, if the event log page model has a property called 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>
Note that I've:
- Changed the
Inherits
bit of the@Page
directive to specify the model type and, - Added the
model
argument toRenderPartial
.
You would of course need to populate Model.NotesModel
in your controller. If it's null
you'll see the same bug you presently see.
精彩评论