How to Represent a DataTable in an MVC View Without knowing the properties of the DataTable

1

This is what I have in the controller, I create a test class, I convert it to json and send that json to the class that converts any json into a DataTable.

var Personas = new List<Personas>();
Personas.Add(new Personas() { Nombre= "Juan", Apellido="Lopez", Edad=30, FechaNacimiento = new DateTime(1987,01,25)});
Personas.Add(new Personas() { Nombre = "Elvin", Apellido = "Acevedo", Edad = 26, FechaNacimiento = new DateTime(1990, 06, 04) });
Personas.Add(new Personas() { Nombre = "Samuel", Apellido = "Acevedo", Edad = 26, FechaNacimiento = new DateTime(1991, 03, 26) });
Personas.Add(new Personas() { Nombre = "Carmen Rosa", Apellido = "Rodriguez", Edad = 23, FechaNacimiento = new DateTime(1987, 01, 25) });
Personas.Add(new Personas() { Nombre = "Pedro", Apellido = "Almanzar", Edad = 36, FechaNacimiento = new DateTime(1987, 08, 24) });
Personas.Add(new Personas() { Nombre = "Maria", Apellido = "Rodriguez", Edad = 19, FechaNacimiento = new DateTime(1998, 12, 25) });

var JsonPersonas = JsonConvert.SerializeObject(Personas);
var dtpPersonas = Utilidades.ObtenerDataTabledeJson(JsonPersonas);

return View(dtpPersonas);

This is the Class that converts the Json TO DataTable

public static DataTable ObtenerDataTabledeJson(string Json)
{

        var jsonDeserializado = JsonConvert.DeserializeObject(Json);
        var jarray = jsonDeserializado as JArray;

        var TituloColumnas = jarray.Children().Children().Select(x => ((JProperty)x).Name).Distinct().ToList();

        var dt = new DataTable();
        foreach (var columna in TituloColumnas)
        {
            dt.Columns.Add(columna);
        }

        foreach (var elemento in jarray)
        {
            var valores = elemento.Children().Select(x => ((JProperty)x).Value.ToObject<object>()).ToArray();
            dt.Rows.Add(valores);
        }
        return dt;
}

And I would like to represent that DataTable in an MVC view without knowing the properties of said dateTable.

Vista.

@{
    ViewBag.Title = "Home Page";
}

<h3>Datos De Personas</h3>
<table>
    <thead>
        <tr>
           <th></th> 
        </tr>
    </thead>
    <tbody>
        <tr>
            <td></td>
        </tr>
    </tbody>

</table>
    
asked by Elvin Acevedo 06.02.2017 в 15:27
source

2 answers

1

In your view you can put something like the following:

@model System.Data.DataTable
@{
    ViewBag.Title = "Home Page";
}
<table border="1">
    <thead>
        <tr>
            @foreach (System.Data.DataColumn columna in Model.Columns)
            {
                <td>@columna.ColumnName</td>
            }
        </tr>        
    </thead>
    <tbody>
        @foreach (System.Data.DataRow fila in Model.Rows)
        {
            <tr>                
                @foreach (System.Data.DataColumn columna in Model.Columns)
                {
                    <td>@fila[columna.ColumnName]</td>
                }
            </tr>
        }
    </tbody>
</table>
    
answered by 06.02.2017 / 17:14
source
1

I do not know why you are using DataTable , but you are missing a MVC's great potential . You could directly pass your People list to the view and use the MVC templates in conjunction with DataAnnotations .

I hope you serve, greetings!

    
answered by 09.02.2017 в 13:55