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

纵饮孤独 提交于 2019-11-27 19:00: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);
}

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.

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)
%>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!