开发者

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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜