-1

thats my Controller Code :

 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");
        }

    }

and thats my ViewPage Code:

@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>

thank you for your attention, i have a product in my main page, when i push On Details product button i want Redirect in this View model(ShowProduct.csHtml) and thats work! and i wanna see my Product Details , i use IEnumerable cause i wanna use forech for showing my another product (Releated Product) on Bottom of my ShowProduct Page but when i run my project and pushed on details product bottun i redirected in Showproduct View Model but this error come up for me :

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]'.
Mehran
  • 45
  • 1
  • 6
  • Why you're using `Single` if you want to iterate `IEnumerable`? Just use `ToList()` after `Where` and you will get `IEnumerable` collection from the model. – Tetsuya Yamamoto Jan 24 '19 at 09:13
  • Possible duplicate of [The model item passed into the dictionary is of type .. but this dictionary requires a model item of type](https://stackoverflow.com/questions/40373595/the-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary-requ) – Tetsuya Yamamoto Jan 24 '19 at 09:14
  • Try return View(src.Select(s=>s).First()). You are not returning IEnumerable rather EF's dynamic proxies. If you should return a List of IEnumerable, read here https://stackoverflow.com/questions/3628425/ienumerable-vs-list-what-to-use-how-do-they-work – curious.netter Jan 24 '19 at 09:18

1 Answers1

0

If you want to show all your products, why are you only passing src.Single() ?

Your view is also expecting an IEnumerable, but only getting a single.

I'd change

var src = blProduct.Where(p => p.Url.Trim() == productName.Trim());

to

List<Product> src = blProduct.Where(p => p.Url.Trim() == productName.Trim()).ToList();

and pass it into the view:

if (src.Any())
        {
            return View(src);
        }
Fross
  • 122
  • 13