Does anyone know how to pass data from a class to a td tag to form a table?

0
<table id="list" class="bordered">
    <tr>
        <th>Nombre</th>
        <th>Apellido Materno</th>
        <th>Apellido Paterno</th>
    </tr>
    <tr>
        <td></td>
    </tr>  
 </table> 

That esl code of the table and what I want to pass are these data of this class

public ActionResult Index(){
        List<Persona> lista = new List<Persona>();

        Persona per1 = new Persona();
        per1.codigo = 1;
        per1.nombre = "Luthien";
        per1.apepat = "Lopez";
        per1.apemat = "Perez";

        lista.Add(per1);

        Persona per2 = new Persona();
        per2.codigo = 2;
        per2.nombre = "Beren";
        per2.apepat = "Beltran";
        per2.apemat = "Caceres";

        lista.Add(per2);

        Persona per3 = new Persona();
        per3.codigo = 3;
        per3.nombre = "Jorge";
        per3.apepat = "Ramirez";
        per3.apemat = "Castillo";

        lista.Add(per3);

        return View(lista);
}
    
asked by pj2016 27.12.2016 в 13:56
source

2 answers

1

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.

    
answered by 04.01.2017 в 17:31
0

From the view, you have access to that list through the Model object.

First, add the @model declaration to the beginning of the view to specify the data type of Model .

@model IEnumerable<TuEspacioDeNombres.Persona>

Then, you can do something like the following:

<table id="list" class="bordered">
    <tr>
        <th>Nombre</th>
        <th>Apellido Materno</th>
        <th>Apellido Paterno</th>
    </tr>
    @foreach (Persona persona in Model)
    { 
        <tr>
            <td>@persona.nombre</td>
            <td>@persona.apemat</td>
            <td>@persona.apepat</td>
        </tr>
     }  
 </table>

You should also add

    
answered by 27.12.2016 в 14:23