当试图遍历从控制器返回的视图中的数据时,我得到一个错误“'object'不包含'CustomerID'的定义并且没有扩展方法'CustomerID'接受'object'类型的第一个参数可以被发现”
这是我的看法
<% using (Html.BeginForm()) {%>
<%foreach (var item in (IEnumerable)Model)
{ %>
<%= Html.Encode(item.CustomerID) %>
<%} %>
<% } %>
这是控制器:
public ActionResult Index()
{
Models.NorthwindDataContext nw = new Models.NorthwindDataContext();
var qry = from ord in nw.Orders
join cust in nw.Customers on ord.CustomerID equals cust.CustomerID
select new Models.OrdersModel
{
CustomerID = ord.CustomerID,
OrderID = ord.OrderID,
OrderDate = ord.OrderDate.Value,
ShipCountry = ord.ShipCountry
};
var ordrs = qry.ToList();
return View(ordrs);
}
这是我的课
public class OrdersModel
{
[Required]
[Display(Name = "OrderID")]
public int OrderID { get; set; }
[Required]
[Display(Name = "OrderDate")]
public DateTime OrderDate { get; set; }
[Required]
[Display(Name = "CustomerID")]
public string CustomerID { get; set; }
[Display(Name = "ShipCountry")]
public string ShipCountry { get; set; }
}