I created a class from a database stored procedure using reflection and I want to be able to display the contents of the data table unto a razor view without using entities or having to create a strongly typed model for the class. Right now I’m doing the following:
public static List<object> GetPeople()
{
DataTable dt = DataAccess.GetPersons();
List<object> obj = MapDataTableToPerson(DataAccess.GetPersons());
return obj;
}
private static List<object> MapDataTableToPerson(DataTable dt)
{
List<object> returnClassObject = new List<object>();
Type typeClass = CreateClassFromDT(dt);
foreach (DataRow dr in dt.Rows)
{
object obj = Activator.CreateInstance(typeClass);
foreach(string columnName in ColumnNames)
{
PropertyInfo property = typeClass.GetProperty(columnName);
property.SetValue(obj, dr[columnName], null);
}
returnClassObject.Add(obj);
}
return returnClassObject;
}
I call GetPeople from the controller and pass unto a view, but I have no clue on how to get the values from the created class in Razor, if anyone can help it will be greatly appreciated.
Pass the DataTable model to the view and loop through the columns and rows. I don’t think you need to use reflection, just get a DataTable from your database.