The model item is of type CookMeIndexViewModel, but requires a model item of type IEnumerable<CookMeIndexViewModel>
Asked Answered
A

3

32

I am following along with the music store example to try learn ASP.NET MVC. I'm creating a cookbook application.

I have created my viewmodel that looks like this:

namespace CookMe_MVC.ViewModels
{
    public class CookMeIndexViewModel
    {
        public int NumberOfReceipes { get; set; }
        public List<string> ReceipeName { get; set; }
    }
}

my controller looks like this

public ActionResult Index()
    {
        var meals= new List<string> { "Dinner 1", "Dinner 2", "3rd not sure" };
       //create the view model
        var viewModel = new CookMeIndexViewModel
        {
            NumberOfReceipes = meals.Count(),
            ReceipeName = meals
        };
        return View(viewModel);
    }

Finally my view looks like this

 @model IEnumerable<CookMe_MVC.ViewModels.CookMeIndexViewModel>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th></th>
        <th>
            Meals
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
        <td>
            @item.ReceipeName
        </td>
    </tr>
}

</table>

I get this error.

The model item passed into the dictionary is of type CookMeIndexViewModel, but this dictionary requires a model item of type IEnumerable<CookMeIndexViewModel>.

I have followed the example. I can't see what I am doing wrong. Should I be returning my viewmodel as a generic list?

Alcibiades answered 27/1, 2011 at 6:16 Comment(0)
T
51

In your view you are using @model IEnumerable<CookMe_MVC.ViewModels.CookMeIndexViewModel> which indicates that the model expected by the View is of type IEnumerable of CookMeIndexViewModel.

However in the controller you are passing an object of type CookMeIndexViewModel as a model return View(viewModel); hence the error.

Either change the view to have @model CookMe_MVC.ViewModels.CookMeIndexViewModel

or pass a IEnumerable of CookMeIndexViewModel as model to the view in controller as given below:

public ActionResult Index()
{
        var meals= new List<string> { "Dinner 1", "Dinner 2", "3rd not sure" };
     //create the view model
        var viewModel = new CookMeIndexViewModel
        {
                NumberOfReceipes = meals.Count(),
                ReceipeName = meals
        };
        List<CookMeIndexViewModel> viewModelList = new List<CookMeIndexViewModel>();
        viewModelList.Add(viewModel);
        return View(viewModelList);
}
Thoreau answered 27/1, 2011 at 6:25 Comment(5)
thanks cybernate. Sorry I havent worked with Ienurmerables before how would I create one in this case?Alcibiades
In my case I forgot to assign a value to the model inside the viewmodel. THe same error occured - But this answer led me in the right direction. Thanks +1Szabo
@Thoreau : I am also having the same error. But I am trying to render a partial view. This is the link to the question. I am passing the values to the view from the controller correctly i guess. But from the Partial view it throws the error. can you please help?Northeastward
@ViniVasundharan Please check the answer from Greg for your original queston. It should help you fix the issue.Thoreau
@chandu . Sure. I will check it. Thanks for your time.Northeastward
C
3

I got this message when I had a conflict between what the @model directive in the _Layout.cshtml layout view and an "inner page" view.

The _Layout.cshtml had directive..

@model MyProject.Models.MyObject

My inner page had...

@model IEnumerable<MyProject.Models.MyObject>

I was working on some test / experiment code and hit this issue when I created new controller etc. It was only when I renamed Model Object and compiled afterwards that I found the source of the problem.

Hope this helps.

Q.

Chance answered 21/8, 2013 at 7:6 Comment(0)
D
-1

in kendo ui Grid do :

public class BookBean
    {
        [ScaffoldColumn(false)]
        public Int32 Id { set; get; }

        public String Title { set; get; }

        public String Author { set; get; }

        public String Publisher { set; get; }

        [UIHint("Integer")]
        public Int32 Price { set; get; }

        [UIHint("Integer")]
        public Int32 Instore { set; get; }

        [UIHint("Integer")]
        public Int32 GroupId { get; set; }
    }

in Integer.ascx in Shared/EditorTemplate folder do :

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<int?>" %>

<%: Html.Kendo().IntegerTextBoxFor(m => m)
      .HtmlAttributes(new { style = "width:100%" })
      .Min(int.MinValue)
      .Max(int.MaxValue)
%>
Dove answered 26/8, 2013 at 14:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.