简体   繁体   中英

ASP.NET MVC5 database

How can I view Sklep data with specified id.

I can see id's of Sklep and Miejsce from Sklep_has_Miejsce with specified number id of Miejsce, but cant view items from Sklep table with id from parameter.

I got

Cannot implicitly convert type 'Sklepy.Models.DB.Sklep' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?) error.

 public ActionResult Index(int? id)
{
    var viewModel = new ProduktIndexData();
    viewModel.Miejsces = db.Miejsces
        .Include(i => i.Sklep_has_Miejsce.Select(s => s.Sklep))
        .OrderBy(i => i.Nazwa);

   if (id !=null)
    {
        ViewBag.MiejsceIDa = id.Value;
        viewModel.Sklep_has_Miejsces = viewModel.Miejsces.Where(i => i.MiejsceID == id.Value).Single().Sklep_has_Miejsce; // works good
        //viewModel.Skleps = viewModel.Sklep_has_Miejsces.Where(i => i.MiejsceID == id.Value).Single().Sklep; //error
    }
        return View(viewModel);
}

From the looks of it, ProductIndexData.Skleps is an IEnumerable<Sklep> and you are attempting to assign a Sklep to it. This can be solved by changing your non-working line to the following:

viewModel.Skleps = viewModel.Sklep_has_Miejsces.Where(i => i.MiejsceID == id.Value).Select(s => s.Sklep);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM