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>
です。
私はその例に従いました。何が間違っているのかわかりません。ビューモデルを汎用リストとして返す必要がありますか?