0

I am trying to send Oracle data from my controller to my view. I understand that the data can be added from the OracleDataReader to a DataTable like so:

OracleDataReader dr = cmd.ExecuteReader();

            if (dr.HasRows)
            {
                dataTable.Load(dr);
            }

Then my view needs to reference a model but I am not sure which. This is what I have tried:

@model System.Data.DataSet
...
@foreach (DataRow row in Model.Tables)
{

}

It doesn't compile, and I am unsure if I am using the correct model, or just incorrect syntax.

Also, I would be interested if this is the most efficient way to write this (should I be using a model? Is there a better option than using DataTable?)

Mr Man
  • 1,498
  • 10
  • 33
  • 54
  • 2
    It doesn't have to do with a model. You need to add a missing library with a using statement at top of your module. You may also need to add the reference to the library from the menu : Project : Add Reference. – jdweng Nov 11 '15 at 14:10
  • You can use the ViewBag. This way you don't have to set the DataSet as the model (which I believe not to be a good practice, btw) – Vitor Rigoni Nov 11 '15 at 14:20
  • @jdweng in the controller (top code window), I have 'using System.Data;' and it compiles. The view is what doesn't compile. – Mr Man Nov 11 '15 at 14:25
  • @VitorRigoni the data I get from the dataReader is 4 rows of 3 columns each. You're saying that using ViewBag is a better practice in this case? – Mr Man Nov 11 '15 at 14:27
  • Why do you need a view? If you are using a form then add a DataGridview and then use datagridview1.Datasource = datatable; – jdweng Nov 11 '15 at 17:07
  • IMHO, yes. The `@model` is supposed to be a strongly typed class (much like a POCO entity). Another (maybe even better) possibility is creating a class that will be your ViewModel, and set this viewmodel as your `@model`. The accepted answer on this question is a clear example: [http://stackoverflow.com/questions/18608452/mvc-4-how-pass-data-correctly-from-controller-to-view](http://stackoverflow.com/questions/18608452/mvc-4-how-pass-data-correctly-from-controller-to-view) – Vitor Rigoni Nov 11 '15 at 18:37

0 Answers0