【发布时间】:2015-07-09 07:16:13
【问题描述】:
我收到错误:对象不包含“样式代码”的定义。事实上,当我在断点上检查它时,我得到了模型中的记录列表我错过了什么?
HomeController.cs
public ActionResult Index()
{
using (var context = new ApronPointEntities())
{
var styles = (from s in context.STYLEs
join
c in context.STYLEPRICEs on s.STYLEID equals c.STYLEID
select new {
STYLEID=s.STYLEID,
STYLECODE=s.STYLECODE,
STYLENAME= s.STYLENAME,
PRICE= c.REGULARPRICE
}).Take(10).OrderByDescending(s =>s.STYLEID).ToList();
return View(styles);
}
}
-------------Index.cshtml--------------
@foreach(var item in Model)
{
<div class="col-md-3 col-xs-6">
<div class="product">
<div class="product-cover">
<span class="product-action">
<span class="product-new">New</span>
<span class="product-sale">Sale</span>
</span>
<div class="product-cover-hover"><span><a href="product.html">View</a></span></div>
<img src="~/Images/Styles/Img-0.jpg" />
</div>
<div class="product-details">
<h1><a href="product.html">@item.STYLECODE</a></h1>
<p>@item.STYLENAME</p>
<div class="product-price">
<i class="icon-257" title="add to cart"></i>
@item.PRICE
</div>
</div>
</div>
</div>
}
【问题讨论】:
-
你正在返回一个匿名对象的集合,创建一个代表它的类会很棒!
-
@StuartLC 它是一个列表 Model ={System.Collections.Generic.Listf__AnonymousType1
>}
标签: c# asp.net asp.net-mvc-4