那是我的控制器代码:
public ActionResult ShowProducts(string productName)
{
ProductRepository blProduct = new ProductRepository();
var src = blProduct.Where(p => p.Url.Trim() == productName.Trim());
if (src.Any())
{
return View(src.Single());
}
else
{
return RedirectToAction("Index");
}
}
这就是我的ViewPage代码:
@model IEnumerable<GymInternetShop.Models.DomainModels.Product>
@foreach (var item in Model)
{
<div class="container" dir="rtl">
<div class="card">
<div class="container-fliud">
<div class="wrapper row">
<div class="preview col-md-6">
<div class="preview-pic tab-content">
<div class="tab-pane active" id="pic-1"><img src="/Files/UploadImages/@item.Image" /></div>
<div class="tab-pane" id="pic-2"><img src="/Files/UploadImages/@item.Image" /></div>
<div class="tab-pane" id="pic-3"><img src="/Files/UploadImages/@item.Image" /></div>
<div class="tab-pane" id="pic-4"><img src="/Files/UploadImages/@item.Image" /></div>
<div class="tab-pane" id="pic-5"><img src="/Files/UploadImages/@item.Image" /></div>
</div>
<ul class="preview-thumbnail nav nav-tabs">
<li class="active"><a data-target="#pic-1" data-toggle="tab"><img src="/Files/UploadImages/@item.Image" /></a></li>
<li><a data-target="#pic-2" data-toggle="tab"><img src="/Files/UploadImages/@item.Image" /></a></li>
<li><a data-target="#pic-3" data-toggle="tab"><img src="/Files/UploadImages/@item.Image" /></a></li>
<li><a data-target="#pic-4" data-toggle="tab"><img src="/Files/UploadImages/@item.Image" /></a></li>
<li><a data-target="#pic-5" data-toggle="tab"><img src="/Files/UploadImages/@item.Image" /></a></li>
</ul>
</div>
<div class="details col-md-6">
<h3 class="product-title">@item.Name</h3>
<div class="rating">
<div class="stars">
<span class="fa fa-star checked"></span>
<span class="fa fa-star checked"></span>
<span class="fa fa-star checked"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
</div>
</div>
<p class="product-description">@Html.DisplayFor(model => item.Summery)</p>
<h4 class="price">قیمت: <span>@item.Price.ToPrice()</span></h4>
<div class="action">
<a class="add-to-cart btn btn-default" product="@item.Id">خرید</a>
<button class="like btn btn-default" type="button"><span class="fa fa-heart"></span></button>
</div>
</div>
</div>
</div>
</div>
谢谢您的关注,当我按“详细信息”产品按钮时,我的主页上有一个产品,我要在此视图模型中重定向(ShowProduct.csHtml),这样就可以了!我想查看我的产品详细信息,所以我使用IEnumerable
,因为我想使用Forech在ShowProduct页面的底部显示我的另一个产品(已发布产品),但是当我运行我的项目并按下详细信息时,我将产品bottun重定向到Showproduct View Model,但出现此错误对我来说:
The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.Product_9F73B4E1D7A93B6E9A923F87D780EE891805309BEF7B2609BEB9BC3DE1B25A33', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[GymInternetShop.Models.DomainModels.Product]'.
答案 0 :(得分:0)
如果要显示所有产品,为什么只传递src.Single()?
您的视图也期望IEnumerable,但只能获得一个。
我要更改
var src = blProduct.Where(p => p.Url.Trim() == productName.Trim());
到
List<Product> src = blProduct.Where(p => p.Url.Trim() == productName.Trim()).ToList();
并将其传递给视图:
if (src.Any())
{
return View(src);
}