user3274859
user3274859

Reputation: 1

MVC 3 details view strong type

hi i am having trouble fetching the detail of my product, the code Sneaker snkr=_sneaker_categoryDataModel.sneakers.Find(id); does not seem to work it saying that the .Find 'does not contain a definition for find'

public class HomeController : Controller
{
    //
    // GET: /Home/
    private Forest.Models.ForestDBEntities
        _music_categoryDataModel = new Forest.Models.ForestDBEntities();
    //private Models.music_categories music_categoryToCreate;
    public ActionResult Index()
    {
        IList<Forest.Models.music_categories> Categories = _music_categoryDataModel.music_categories.ToList<Forest.Models.music_categories>();
        return View("Categories",Categories);
    }

    public ActionResult Details (int id)
    {
        Sneaker snkr=_sneaker_categoryDataModel.sneakers.Find(id);

        return View(snkr);
    }
}

Upvotes: 0

Views: 54

Answers (1)

James
James

Reputation: 82096

_sneaker_categoryDataModel.sneakers is most likely going to be an IQueryable<> / IEnumerable<> which don't have a method called Find. Find is a method on List<T>, so the only way you can use this would be to call ToList() before Find (which can lead to negative performance implications depending on the collection).

There are various LINQ extensions which make querying these types of collections trivial, SingleOrDefault or FirstOrDefault would be an adequate choice here

Sneaker snkr = _sneaker_categoryDataModel.sneakers.FirstOrDefault(x => x.Id == id);

Upvotes: 2

Related Questions