shozo lojacoono

shozo lojacoono

  • NA
  • 18
  • 717

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(); //<---???
 
//or
 
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;}
 
 
 
 
VediDevice.cshtml:

@model List<MVCControlToolkit.Models.Device>


@{
ViewBag.Title = "VediDevice";
}

<h2>VediDevice</h2>

<table class="table table-striped table-condensed">
<tr>
<th>
NOME
</th>

</tr>

@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.nome)
</td>

</tr>
}

</table>
 
 
 

Not work.

What should I do?

Thank you

Ale

 



Answers (12)