【发布时间】:2023-03-25 17:45:01
【问题描述】:
我正在跟随音乐商店示例来尝试学习 ASP.NET MVC。我正在创建一个食谱应用程序。
我创建了如下所示的视图模型:
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>类型的模型项。
我遵循了这个例子。我看不出我做错了什么。我应该将我的视图模型作为通用列表返回吗?
【问题讨论】:
标签: asp.net asp.net-mvc-3 asp.net-mvc-viewmodel