我收到错误消息:对象不包含“ 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>
}
我想建议你参加一个班,即 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
文件
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句