本文介绍了模型项的类型为 CookMeIndexViewModel,但需要类型为 IEnumerable<CookMeIndexViewModel> 的模型项.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在跟随音乐商店示例尝试学习 ASP.NET MVC.我正在创建一个食谱应用程序.

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);
    }

最后我的观点是这样的

 @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>

我收到此错误.

传递给字典的模型项是 CookMeIndexViewModel 类型,但此字典需要 IEnumerable 类型的模型项.

我遵循了这个例子.我看不出我做错了什么.我应该将我的视图模型作为通用列表返回吗?

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

推荐答案

在您的视图中,您正在使用 @model IEnumerable 这表明 View 期望的模型是CookMeIndexViewModel 的 IEnumerable 类型.

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.

但是在控制器中,您将 CookMeIndexViewModel 类型的对象作为模型传递 return View(viewModel); 因此出现错误.

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

将视图更改为具有 @model CookMe_MVC.ViewModels.CookMeIndexViewModel

或将 CookMeIndexViewModel 的 IEnumerable 作为模型传递给控制器​​中的视图,如下所示:

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);
}

这篇关于模型项的类型为 CookMeIndexViewModel,但需要类型为 IEnumerable<CookMeIndexViewModel> 的模型项.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 17:56