Oskar Ahlberg
Oskar Ahlberg

Reputation: 21

Cannot implicitly convert type, an explicit conversion exists (are you missing a cast?)

I've set up a ViewModel (was previously using a ViewBag) but I've encountered an error in the following code, I've researched it but haven't personally been able to figure out my issue:

public ActionResult Index(string category, string search)
    {
        ProductIndexViewModel viewModel = new ProductIndexViewModel();
        var products = db.Products.Include(p => p.Category);

        if (!String.IsNullOrEmpty(search))
        {
            products = products.Where(p => p.Name.Contains(search) ||
                    p.Description.Contains(search) ||
                    p.Category.Name.Contains(search));

            viewModel.Search = search;

        }

        viewModel.CatsWithCount = from matchingProducts in products
                                  where matchingProducts.CategoryID != null
                                  group matchingProducts by
                                  matchingProducts.Category.Name into
                                  catGroup
                                  select new CategoryWithCount()
                                  {
                                      CategoryName = catGroup.Key,
                                      ProductCount = catGroup.Count()
                                  };

        if (!String.IsNullOrEmpty(category))
        {
           products = products.Where(p => p.Category.Name == category);
        }

        viewModel.Products = products;
        return View(viewModel);
    }

The error occurs down the bottom in the line:

viewModel.Products = products;

The exact error is

"Cannot implicitly convert type 'System.Linq.IQueryable<OnlineStore.Models.Product>' to 'System.Linq.IQueryable<OnlineStore.ViewModels.ProductIndexViewModel>'. An explicit conversion exists (are you missing a cast?)"

I'm very new to using Visual Studio and was just wanting to know what I would have to do to fix this error.

EDIT:

ProductIndexViewModel:

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OnlineStore.ViewModels
{
    public class ProductIndexViewModel
    {
        public IQueryable<ProductIndexViewModel> Products { get; set; }
        public string Search { get; set; }
        public IEnumerable<CategoryWithCount> CatsWithCount { get; set; }
        public string Category { get; set; }
        public IEnumerable<SelectListItem> CatFilterItems
        {
            get
            {
                var allCats = CatsWithCount.Select(cc => new SelectListItem
                {
                    Value = cc.CategoryName,
                    Text = cc.CatNameWithCount
                });
                return allCats;
            }
        }
    }
    public class CategoryWithCount
    {
        public int ProductCount { get; set; }
        public string CategoryName { get; set; }
        public string CatNameWithCount
        {
            get
            {
                return CategoryName + " (" + ProductCount.ToString() + ")";
            }
        }
    }
}````

Upvotes: 1

Views: 7108

Answers (2)

Oskar Ahlberg
Oskar Ahlberg

Reputation: 21

The problem was in my ProductIndexViewModel class. I had

public IEnumerable<ProductIndexViewModel> Products { get; set; } 

instead of

public IEnumerable<Product> Products { get; set; }

Thank you to everyone who answered.

Upvotes: 1

TheGeneral
TheGeneral

Reputation: 81493

Your Products seem wrong in the ProductIndexViewModel class

public class ProductIndexViewModel
{
    public IQueryable<ProductIndexViewModel> Products { get; set; }

  1. Products is an IQueryable, which doesn't really make sense, and probably should be a List<T> or IEnumerable<T>

Example

public List<ProductIndexViewModel> Products { get; set; }

  1. The generic parameter looks wrong, it likely should be Product or some ViewModel like ProductViewModel in the case of a ViewModel you will need to Select/Project to it

  1. You should probably be calling, ToList on the results

Example

viewModel.Products = products.ToList();

Upvotes: 0

Related Questions