我收到错误:对象没有包含“StyleCode”的定义。事实上,我在断点检查时得到了模型中的记录列表我缺少什么?
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>
}
答案 0 :(得分:0)
@foreach(var item in Model)
'var'解决的类型是什么?
这很可能是一个对象。您应该先将其转换为您的类型,然后才能访问其某些成员。
答案 1 :(得分:0)
我建议您制作一个类ViewModel
public class ViewModel
{
public int STYLEID { get; set; }
public string STYLECODE { get; set; }
public string STYLENAME { get; set; }
public double PRICE { get; set; }
}
并将List<ViewModel>
提供给.cshtml
文件