Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with Partial Views in ASP.NET MVC

Background

I'm receiving the following error when trying to render a partial view in ASP.NET MVC. I am new to ASP.NET MVC and am sure the error is simple to resolve and just stems from my lack of complete understanding.

Question (for those not wanting to read everything):

What is causing this error ?

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'MyApp.Models.ClassroomFormViewModel' but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[MyApp.Models.ClassroomFormViewModel]'`.


Entites

I have two entities with a parent/child relationship.

Classroom                   StickyNote 
------------                -----------
Id          1 -----         Id
Name               \        Name
(...)               \       Content
                     ---- * ClassroomID

Model

In the Model the StickyNote Content is kept in a different table, and accessed (using Linq-to-SQL through the following method:

public IQueryable<StickyNote> GetStickyNotesByClassroom(Classroom classroom)
{
     return from stickynote in db.StickyNotes
            where stickynote.ClassroomID == classroom.ID
            select stickynote;
}

Error

I've created a partial view for displaying StickyNote content since it 'belongs' to the classroom it's on. The problem I'm running into is that I'm not able to get it to display, and receive the following error:

The model item passed into the dictionary is of type: 'MyApp.Models.ClassroomFormViewModel' but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[MyApp.Models.ClassroomFormViewModel]'`. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'MyApp.Models.ClassroomFormViewModel' but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[MyApp.Models.ClassroomFormViewModel]'`.

Partial View

Here is the partial view code:

<%@ Control Language="C#" Inherits="
System.Web.Mvc.ViewUserControl<IEnumerable<MyApp.Models.ClassroomFormViewModel>>" %>

    <table background="../../images/corkboard.jpg">

    <% foreach (var items in Model) { %>

        <tr>
        <% foreach (var item in items.StickyNotes) { %>
            <td><div class="sticky_note_container">

<!-- actually use a post it note here on the page -->
<div class="sticky_note">
<div class="sticky_note_content">
<!-- content of sticky note here -->
<% Html.ActionLink(item.Name, "ShowStickyNoteContent"); %>
<!-- end of content of sticky note -->
</div>
</div>
<div class="sticky_note_footer">&nbsp;</div>
<br clear="all" />
</div>
         </td>
      <% } %>
     </tr>
   <% } %>
</table>

Parent View

And the code from the other View that calls it:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits=
"System.Web.Mvc.ViewPage<MyApp.Models.ClassroomFormViewModel>" %>
{...}
  <% 
     Html.RenderPartial("StickyNotes", Model);
  %>
like image 691
George Stocker Avatar asked Jul 09 '09 13:07

George Stocker


2 Answers

You are passing in a single instance of your ClassroomFormViewModel into and the View is expecting a a collection i.e. IEnumerable<ClassroomFormViewModel>.

Change your declaration in your PartialView to

Inherits="
System.Web.Mvc.ViewUserControl<MyApp.Models.ClassroomFormViewModel>"

OR

What you really want (after really looking at your code) IS an IEnumerable<ClassroomFormViewModel>

so your Model in your calling page needs to be of IEnumerable<ClassroomFormViewModel>

Essentially you're trying to do this

public void Render(ClassroomFormViewModel model)
{
    RenderPartial(model) //Cannot cast single instance into an IEnumerable
}
public string RenderPartial(IEnumerable<ClassroomFormViewModel> model)
{
    //Do something
}
like image 68
Micah Avatar answered Nov 13 '22 02:11

Micah


Your partial should start

<%@ Control Language="C#" Inherits="
System.Web.Mvc.ViewUserControl<MyApp.Models.ClassroomFormViewModel>" >

I guess you want to display one classroom on you page. If you want to display more then dont use a list of viewmodels. Use one viewmodel that has a list of classrooms

like image 25
Mathias F Avatar answered Nov 13 '22 02:11

Mathias F