2011-01-27 28 views
32

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ẫu IEnumerable<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?

Trả lời

51

Trong chế độ xem của bạn, bạn đang sử dụng @model IEnumerable<CookMe_MVC.ViewModels.CookMeIndexViewModel> cho biết mô hình mà Chế độ xem dự kiến ​​là loại IEnumerable của CookMeIndexViewModel.

Tuy nhiên trong bộ điều khiển, bạn đang truyền một đối tượng kiểu CookMeIndexViewModel làm mẫu return View(viewModel); do đó lỗi.

Hoặc thay đổi quan điểm để có @model CookMe_MVC.ViewModels.CookMeIndexViewModel

hoặc vượt qua một IEnumerable của CookMeIndexViewModel như mô hình để xem trong bộ điều khiển như đưa ra dưới đâ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 
     }; 
     List<CookMeIndexViewModel> viewModelList = new List<CookMeIndexViewModel>(); 
     viewModelList.Add(viewModel); 
     return View(viewModelList); 
} 
+0

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? –

+0

Cập nhật bài đăng với mã mẫu.Pls chk – Chandu

+0

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

3

tôi đã thông báo này khi tôi đã có một cuộc xung đột giữa những gì chỉ thị @model trong chế độ xem bố cục _Layout.cshtml và chế độ xem "trang bên trong".

Các _Layout.cshtml có chỉ thị ..

@model MyProject.Models.MyObject 

trang bên trong tôi đã ...

@model IEnumerable<MyProject.Models.MyObject> 

Tôi đã làm việc trên một số kiểm tra mã/thử nghiệm và đạt vấn đề này khi tôi tạo mới bộ điều khiển vv. Chỉ khi tôi đổi tên Model Object và biên dịch sau đó tôi mới tìm ra nguồn gốc của vấn đề.

Hy vọng điều này sẽ hữu ích.

Q.

+0

Làm việc cho tôi quá .. Cảm ơn bạn! – Gurusinghe

1

trong Lưới ui kendo làm:

public class BookBean 
    { 
     [ScaffoldColumn(false)] 
     public Int32 Id { set; get; } 

     public String Title { set; get; } 

     public String Author { set; get; } 

     public String Publisher { set; get; } 

     [UIHint("Integer")] 
     public Int32 Price { set; get; } 

     [UIHint("Integer")] 
     public Int32 Instore { set; get; } 

     [UIHint("Integer")] 
     public Int32 GroupId { get; set; } 
    } 

trong Integer.ascx trong thư mục Shared/EditorTemplate làm:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<int?>" %> 

<%: Html.Kendo().IntegerTextBoxFor(m => m) 
     .HtmlAttributes(new { style = "width:100%" }) 
     .Min(int.MinValue) 
     .Max(int.MaxValue) 
%> 
Các vấn đề liên quan