Pinu
Pinu

Reputation: 7510

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.IEnumerable1[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>

Upvotes: 0

Views: 1299

Answers (1)

Craig Stuntz
Craig Stuntz

Reputation: 126547

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:

  1. Changed the Inherits bit of the @Page directive to specify the model type and,
  2. Added the model argument to RenderPartial.

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.

Upvotes: 3

Related Questions