Reputation: 27
I have two methods where I'm trying to load records from database based on an Id.
These are my methods:
private List<Cliente> GetCLienti(int clienteId) //to load Clients from DB
{
var tuttiClienti = _db.tboSottoClienti//Not sure if this query works
.Where(c => c.Id_cliente == clienteId)
.FirstOrDefaultAsync();
//var tuttiClienti = _db.tboClienti.ToList();
return tuttiClienti; //I need to return LIST
}
private List<SottoCliente> GetSottoCliente(clienteId)
{
var tuttiSottoClienti = _db.tboSottoClienti.ToList();
return tuttiSottoClienti;
}
And here is the method that shows it in the view:
public IActionResult CaricaSottoCliente(int clienteId)
{
ViewBag.Cliente = GetCLienti();
ViewBag.SottoCliente = GetSottoCliente();
return View();
}
This is my view:
<table class="table table-hover">
<tr>
<th>Id</th>
<th>Nome</th>
<th>Cognome</th>
<th>Azienda</th>
<th>Cellulare</th>
</tr>
@foreach (GestioneAtivita.Models.SottoCliente sottoCliente in ViewBag.SottoCliente)
{
<tr>
<td>@sottoCliente.Id</td>
<td>@sottoCliente.Nome</td>
<td>@sottoCliente.Cognome</td>
<td>@sottoCliente.Azienda</td>
<td>@sottoCliente.Cellulare</td>
</tr>
}
</table>
<p><b>Cliente List</b></p>
<table class="table table-hover">
<tr>
<th>Id</th>
<th>Code</th>
<th>Name</th>
<th>Enrollment No</th>
</tr>
@foreach (GestioneAtivita.Models.Cliente cliente in ViewBag.Cliente)
{
<tr>
<td>@cliente.Nome</td>
<td>@cliente.Cognome</td>
<td>@cliente.Nome_azienda</td>
<td>@cliente.Cellulare</td>
</tr>
}
</table>
And finally, this is how I pass ID from another View to the Controller:
<td>
@Html.ActionLink("Sotto Clienti", "CaricaSottoCliente", new { clienteId = item.Id }, new { @class = "btn btn-outline-success" })
</td>
How can I return a List
from the GetCLienti(int clienteId)
method?
Any suggestions? Thanks in advance!
Upvotes: 0
Views: 128
Reputation: 27
This worked for me :
Here I load all data from _db.tboClienti
private List<Cliente> GetCLienti(int clienteId)
{
var tuttiClienti = _db.tboClienti
.Where(c => c.Id == clienteId)
.ToList();
return tuttiClienti;
}
Then I pass it like this :
public IActionResult CaricaSottoCliente(int clienteId)
{
ViewBag.Cliente = GetCLienti(clienteId);
ViewBag.SottoCliente = GetSottoCliente(clienteId);
return View();
}
Upvotes: 1
Reputation: 12619
You are using FirstOrDefaultAsync()
but you are not await
ing it. Neither your method is async
so replace it with FirstOrDefault()
. But as your method as return type as List<Cliente>
you should use .ToList()
like below.
private List<Cliente> GetCLienti(int clienteId) //to load Clients from DB
{
var tuttiClienti = _db.tboSottoClienti//Not sure if this query works
.Where(c => c.Id_cliente == clienteId)
.ToList();
//var tuttiClienti = _db.tboClienti.ToList();
return tuttiClienti; //I need to return LIST
}
You need to pass clienteId
value to GetCLienti
it is having parameter.
public IActionResult CaricaSottoCliente(int clienteId)
{
ViewBag.Cliente = GetCLienti(clienteId);
ViewBag.SottoCliente = GetSottoCliente();
return View();
}
Upvotes: 1
Reputation: 4178
.FirstOrDefaultAsync(); will return only one result. Remove that, or replace it with .ToList() and your code will return a list of items...
Upvotes: 0