我正在跟随音乐商店示例以尝试学习ASP.NET MVC。我正在创建一个食谱应用程序。该模型项是CookMeIndexViewModel类型,但需要IEnumerable类型的一个模型项<CookMeIndexViewModel>
我创建我的视图模型,看起来像这样:
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>
类型的模型项。
我遵循了这个例子。我看不到我做错了什么。我应该将我的viewmodel作为通用列表返回吗?
感谢cybernate。对不起,我没有使用Ienurmerables之前我将如何创建一个在这种情况下? – 2011-01-27 07:24:09
使用示例代码更新了帖子。请注意chk – Chandu 2011-01-27 07:28:50