shozo lojacoono

shozo lojacoono

  • NA
  • 18
  • 707

mvc select second table data using linq

Nov 28 2019 4:30 AM

hallo, I am new in mvc and linq. I've 2 table in relation many to many: 1)employe 2)device

Selecting an employe I'd like to see his devices:

-Marco -Franco Selecting Marco should appear the devices associated to him.

My code (not work):

Nel controller:
public ActionResult VediDevice(int id)
{ Impiegato impi = new Impiegato(); List<Device> ListDevice = new List<Device>(); using (DatabaseMVCCOntrolsEntities db = new DatabaseMVCCOntrolsEntities())
ListDevice= db.Devices.Where(u => db.ImpiegatiDevice.Any(m => m.impiegatiID == id)).ToList(); //<---???
ListDevice= db.Devices.ToList().Where(u => u.item.Any(m => m.impiegatiID == id)).ToList(); //<--??


return View(ListDevice);

In device.cs I added:
List<ImpiegatiDevice> item{get;set;}

@model List<MVCControlToolkit.Models.Device>

ViewBag.Title = "VediDevice";


<table class="table table-striped table-condensed">


@foreach (var item in Model)
@Html.DisplayFor(modelItem => item.nome)



Not work.

What should I do?

Thank you



Answers (12)