Tôi đang theo dõi cùng với ví dụ cửa hàng nhạc để thử tìm hiểu ASP.NET MVC. Tôi đang tạo một ứng dụng sách dạy nấu ăn.Mục mô hình thuộc loại CookMeIndexViewModel, nhưng yêu cầu một mục mẫu kiểu IEnumerable <CookMeIndexViewModel>
Tôi đã tạo ra viewmodel của tôi trông như thế này:
namespace CookMe_MVC.ViewModels
{
public class CookMeIndexViewModel
{
public int NumberOfReceipes { get; set; }
public List<string> ReceipeName { get; set; }
}
}
điều khiển của tôi trông như thế này
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);
}
Cuối cùng quan điểm của tôi trông như thế này
@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>
tôi nhận được lỗi này .
Mục mẫu được chuyển vào từ điển là loại
CookMeIndexViewModel
, nhưng từ điển này yêu cầu mục kiểu mẫuIEnumerable<CookMeIndexViewModel>
.
Tôi đã làm theo ví dụ. Tôi không thể nhìn thấy những gì tôi đang làm sai. Tôi có nên trả lại chế độ xem của mình dưới dạng danh sách chung không?
nhờ cybernate. Xin lỗi tôi havent đã làm việc với Ienurmerables trước khi làm thế nào tôi sẽ tạo ra một trong trường hợp này? –
Cập nhật bài đăng với mã mẫu.Pls chk – Chandu
Trong trường hợp của tôi, tôi quên gán một giá trị cho mô hình bên trong viewmodel. Cùng một lỗi xảy ra - Nhưng câu trả lời này đã dẫn tôi đi đúng hướng. Cảm ơn +1 – ppumkin