Following the model MVC you should have a model (class) with the data structure, in this case, I would have something like that for the model.
//Clase auxiliar con los datos de la persona//
public class Persona
{
private String _nombre;
private String _apellidoMaterno;
private String _apellidoPaterno;
public String Nombre
{
get{return this._nombre;}
set{this._nombre = value;}
}
public String ApeMaterno
{
get{return this._apellidoMaterno;}
set{this._apellidoMaterno = value;}
}
public String ApePaterno
{
get{return this._apellidoPaterno;}
set{this._apellidoPaterno = value;}
}
public Persona(String nombre, String apeMaterno, String apePaterno)
{
this.Nombre = nombre;
this.ApeMaterno = apeMaterno;
this.ApePaterno = apePaterno;
}
}
//Modelo de datos que pasaremos a la vista para generar la tabla con Razor//
public class ListaPersonasModel
{
private List<Persona> _listaPersonas = new List<Persona>();
public List<Persona> lPersonas
{
get{return this._listaPersonas;}
set{this._listaPersonas = value;}
}
}
//CONTROLADOR//
public ActionResult Index()
{
ListaPersonasModel lista = new ListaPersonasModel();
Persona personaUno = new Persona("Luthien", "Lopez","Perez");
Persona personaDos = new Persona("Beren", "Beltran", "Caceres");
Persona personaTres = new Persona("Jorge", "Ramirez", "Castillo");
lista.Add(personaUno);
lista.Add(personaDos);
lista.Add(personaTres);
//Si podemos retornamos una vista y un modelo asociado a dicha vista//
//En este caso, el modelo era una lista de personas
//, por lo que pasamos la lista de personas//
return View("~/Views/NombreVista",lista);
}
<table id="list" class="bordered">
<tr>
<th>Nombre</th>
<th>Apellido Materno</th>
<th>Apellido Paterno</th>
</tr>
@foreach(var persona in Model)
{
<tr>@persona.Nombre</tr>
<tr>@persona.ApeMaterno</tr>
<tr>@persona.ApePaterno</tr>
}
</table>
Finally we paint the view, in which we have to import at the beginning, the data type of the model , not the name of the model.
Whenever I deal with MVC I try to distinguish from the auxiliary data types (class persona
in this case) of the model that I am going to see, because a view can reach it is much more complex, and although it is more laborious, I also try to distinguish between private members and properties, so as not to touch anything deprived of the classes from the viewpoint.