0

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.

4
  • 1
    i am curious to know why you do not want a (model/ viewmodel )class to do this ? Commented Nov 9, 2012 at 0:22
  • The company I'm working for makes a great amount of changes unto databases and they don't want to recompile and publish again every time it changes. Commented Nov 9, 2012 at 0:26
  • If you are going to do this approach for a Large amount of places, It is going to be hard to read the code later. Invest some time now and save money and time Commented Nov 9, 2012 at 0:29
  • It won't be applied to a lot of places, but it will manage a few things. Performance isn't an issue and readability/mantainability isn't one so much either. Commented Nov 9, 2012 at 0:34

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I had not even thought of this..., this makes a lot of sense, will pass it on to my project leader and ask him if this is better. Thank you.
Although for some strange reason one of the parameters isn't being passed correctly, but this is another issue altogether

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.