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>