我有(客户,客户收藏,产品)表。在客户收藏夹表中存储了customerID和PorductID,显示了哪个产品是哪个客户的收藏夹。我想在页面上显示所有带有空心形图标的产品,如果客户有最喜欢的产品,则该产品的图标应为心形图标。
这是我的代码:
控制器:
public ActionResult Index()
{
ProductRepository BlProduct = new ProductRepository();
CustomerRepository BlCustomer = new CustomerRepository();
CustomerLikeRepository BlCusLike = new CustomerLikeRepository();
var username = User.Identity.Name;
var customer = BlCustomer.Where(x => x.Email == username).SingleOrDefault();
var model = new HomeIndexViewModel();
model.Products = BlProduct.Select();
if(customer!=null)
{
model.Customer = customer;
model.CostumerLikes = BlCusLike.Where(l=>l.CustomerId==customer.id).ToList();
}
return View(model);
}
ViewModel:
public class HomeIndexViewModel
{
public IEnumerable<MySIte.Models.DomainModels.Product> Products { get; set; }
public MySIte.Models.DomainModels.Customer Customer { get; set; }
public IEnumerable<MySIte.Models.DomainModels.CostumersLike> CostumerLikes { get; set; }
}
查看:
@foreach (var item in Model.Products)
{
<div class="product-thumb clearfix" style="height:100%;">
<a href="@item.id">
@Html.ImageFor(modelItem => item.ImgUrl, new { width = "500" }, "Tulips.jpg", "Files", "UploadImages")
</a>
</div>
<div class="product-info clearfix">
<span class="product-title">@item.Name</span>
<div class="price">
<del>
<span class="regular">$@item.Price.ToPrice()</span>
</del>
<ins>
<span class="amount">$@item.Price</span>
</ins>
</div>
</div>
<div class="add-to-cart text-center">
<a data-toggle="modal" data-target="#@item.id"><i class="fa fa-search"></i> quick view</a>
</div>
@if (Model.Customer.Email==User.Identity.Name)
{
<a href="#" class="addWish like" data-product="@item.id"><i class="fa fa-heart-o" style="color:red;"></i></a>
}
</li>
}
在视图部分中有一个IF语句。那就是我想将图标更改为“ fa-heart”(如果它是客户最喜欢的地方)的地方。
我如何获取类似于客户的数据,并检查表中是否有customerid和product id。
答案 0 :(得分:0)
我终于知道了如何检查customerliked表中是否存在Customerid和productid。
鉴于此,我必须像下面这样写if语句:
“如果”视图的一部分:
@if (Model.Customer.Email == User.Identity.Name)
{
if (Model.CostumerLikes.Where(x => x.ProductId == item.id && x.CustomerId== Model.Customer.id).Count()==1)
{
<a href="#" class="addWish like" data-product="@item.id" data-user="@Model.Customer.id"><i class="fa fa-heart" style="color:red;"></i></a>
}
else
{
<a href="#" class="addWish like" data-product="@item.id" data-user="@Model.Customer.id"><i class="fa fa-heart-o" style="color:red;"></i></a>
}
}