我跟随音乐store 的例子try 学习ASP.NET MVC.我正在创建一个烹饪书应用程序.
我创建的viewmodel如下所示:
namespace CookMe_MVC.ViewModels
{
public class CookMeIndexViewModel
{
public int NumberOfReceipes { get; set; }
public List<string> ReceipeName { get; set; }
}
}
我的控制器看起来像这样
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<CookMeIndexViewModel>
的模型项.
我已经效仿了这个例子.我看不出我做错了什么.我是否应该将视图模型作为通用列表返回?