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(); //<---???
{ 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);
}
}
return View(ListDevice);
}
In device.cs I added:
List<ImpiegatiDevice> item{get;set;}
VediDevice.cshtml:
@model List<MVCControlToolkit.Models.Device>
@{
ViewBag.Title = "VediDevice";
}
VediDeviceh2>
<table class="table table-striped table-condensed">
NOME
th>
tr>
@foreach (var item in Model)
{
@Html.DisplayFor(modelItem => item.nome)
td>
tr>
}
table>
Not work.
What should I do?
Thank you
Ale
12 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
shozo lojacoonoPosted Nov 29, 2019, 4:20 AM
shozo lojacoonoPosted Nov 29, 2019, 2:20 AM
select Devices.nome from Devices where Devices.id IN(select DeviceID from ImpiegatoDevices where ImpiegatoDevices.impiegatiID=id);
Ashutosh GuptaPosted Nov 28, 2019, 11:14 AM
shozo lojacoonoPosted Nov 28, 2019, 11:00 AM
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Impiegato()
{
this.ImpiegatiDevice = new HashSet
}
public int Id { get; set; }
public string nome { get; set; }
public string cognome { get; set; }
public string email { get; set; }
public Nullable
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection
}
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Device()
{
this.ImpiegatiDevice = new HashSet
}
public int Id { get; set; }
public string nome { get; set; }
public string descrizione { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection
}
{
public int Id { get; set; }
public Nullable
public Nullable
public virtual Device Device { get; set; }
public virtual Impiegato Impiegati { get; set; }
public List
}
Ashutosh GuptaPosted Nov 28, 2019, 10:41 AM
shozo lojacoonoPosted Nov 28, 2019, 10:38 AM
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Device()
{
this.ImpiegatiDevice = new HashSet
}
public int Id { get; set; }
public string nome { get; set; }
public string descrizione { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection
}
Ashutosh GuptaPosted Nov 28, 2019, 10:24 AM
shozo lojacoonoPosted Nov 28, 2019, 9:57 AM
Dhara PatelPosted Nov 28, 2019, 9:28 AM
shozo lojacoonoPosted Nov 28, 2019, 8:16 AM
shozo lojacoonoPosted Nov 28, 2019, 7:13 AM
Errore CS0266 Non è possibile convertire in modo implicito il tipo 'System.Linq.IQueryable
Dhara PatelPosted Nov 28, 2019, 5:11 AM