Tôi có nên tạo một định nghĩa cho GetEnumerator theo cách thủ công không? Dường như nó nên biết ...định nghĩa công khai của GetEnumerator trong asp.net mvc mất tích?
tôi nhận được lỗi sau:
foreach statement cannot operate on variables of type 'MvcAppNorthwind.Models.Product' because 'MvcAppNorthwind.Models.Product' does not contain a public definition for 'GetEnumerator'
Line 9: <h2><%: ViewData["Message"] %></h2>
Line 10: <ul>
Line 11: <% foreach (MvcAppNorthwind.Models.Product p in ViewData.Model) { %>
Line 12: <li><%= p.ProductName %></li>
Line 13: <% } %>
Trong điều khiển của tôi, tôi có mã này:
NorthwindDataContext db = new NorthwindDataContext();
ViewData["Message"] = "Welcome to ASP.NET MVC!";
var products = from p in db.Products
select p;
return View(products);
Tôi đã thay đổi tuyên bố quan điểm của tôi như thế này và giờ nó hoạt động:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcAppNorthwind.Models.Product>>" %>
Nhưng nếu bạn muốn hiển thị hoặc sử dụng dữ liệu từ nhiều mô hình trong cùng một giao diện? Làm thế nào để bạn làm điều đó sau đó?