I am using ASP.NET MVC4 and I find it difficult to bring data stored in the tables in the database.
I understand that there are some clauses ToList()
that could list the data, but currently the methods I use always return me null
.
The code that I am currently implementing is:
Controller (ViewModel):
public ActionResult EditForm()
{
FormularioFlota2 FF2 = new FormularioFlota2();
FF2.FormularioFlota_Persona = GetPersona();
return View(FF2);
}
public DbSet<Persona> DbPersonas { get; set; }
public List<Persona> GetPersona()
{
List<Persona> listaDePersonasPrueba = new List<Persona>();
listaDePersonasPrueba = DbPersonas.ToList();
return listaDePersonasPrueba;
}
View (ViewModel):
@if (Model.FormularioFlota_Persona != null && Model.FormularioFlota_Persona.Count > 0)
{
foreach (var item in Model.FormularioFlota_Persona)
{
<tr>
<td>
@item.Id
</td>
<td>
@item.NumeroLegajo
</td>
<td>
@item.Nombre
</td>
<td>
@item.Apellido
</td>
<td>
@item.Calle
</td>
<td>
@item.Localidad
</td>
<td>
@item.Provincia
</td>
<td>
@item.Telefono
</td>
<td>
@item.Email
</td>
<td>
</td>
<td>
@item.Documento
</td>
<td>
@item.Cuil
</td>
<td>
@item.EstadoCivil
</td>
<td>
@item.Cargo
</td>
<td>
@item.SectorId
</td>
<td>
@item.GrupoBeneficiarioId
</td>
</tr>
}
}
Note: for some reason the
TD
YTR
tags do not detect me as text, that's why in the View You see everything stuck, but basically I walk vectorFormularioFlotaPersona
and assign the fields, that's when I failed, the compiler tells me that he receives "null
"
Does anyone know a method that can be applied in a controller (from viewmodel) to bring all the data stored in a table? I have all the tables defined in different models and working correctly. Ideally, avoid using Queryable commands.