MVC datasets with viewbags

How to place a dataset in a view and display the result in a view?

I have a dataset from a model that I am writing to a bag. I want to use a foreach loop to get datarows from a viewbag in a view.

I already have a variable going into the view, so I cannot pass this dataset normally. I will also have many other datasets per page. so I thought a viewbag is the best way to approach this problem.

model

class modeldata 
{
    public dataset readrows(DataSet dataset)
    {
    //returns data from sql query.
    }
}

      

:

 DataSet data = new DataSet();
 modeldata getdata = new modeldata ();
 ViewBag.Data = getdata.readrows(data);
 return view("page1") //based on case statement. 
 //Already have a value going into view, so I need to use viewbag

      

View:

@Model site.controllers.homecontroller;

     foreach (Model.data row in ViewBag.Data.Rows)
        {
            @:row["id"] + " " + row["name"];
        } 

      

+3


source to share


2 answers


To display data in a view, you have two options. One is to pass an instance of the Model class into a strongly typed view. The second option is to use ViewBag. In your case, it looks like you are doing a bit of both, but I would recommend using a strongly typed view approach.

The view will have a property Model

that represents an instance of the class type specified by your declaration @Model

. In your code, you are using a controller class that will not work. I have rewritten the use case DataSet

as a model. As you can see, the Model

view property becomes an instance of the class System.Data.DataSet

and has all of its properties and methods.

View

@Model System.Data.DataSet;

foreach (DataRow row in Model.Rows)
{
    @:row["id"] + " " + row["name"];
} 

      

controller

DataSet data = new DataSet();
modeldata getdata = new modeldata();
return View(getdata.readrows(data));

      

Edit:



Here's an example that uses a dictionary in a model class to store multiple datasets. Then you can change the view to use the modeldata type as the Model class.

Model

namespace Site.Models
{
    class modeldata
    {
        public Dictionary<string, DataSet> DataSets { get; set; }

        public static DataSet ReadRows(DataSet dataset)
        {
            //returns data from sql query.
        }
    }
}

      

View

@Model Site.Models.modeldata;

@foreach (System.Data.DataTable table in Model.DataSets["sampleData"].Tables)
{
    foreach (System.Data.DataRow row in table.Rows)
    {
        @:row["id"] + " " + row["name"];
    }
} 

      

controller

DataSet data = new DataSet();
modeldata getdata = new modeldata();
getdata.DataSets["sampleData"] = modeldata.ReadRows(data);
return View(getdata);

      

+7


source


You will need to create a model class to hold each dataset as a property, for example:

public class MyModel 
{
    DataSet data1,
    DataSet data2,
    DataSet data3
}

      

Then return a strongly typed view bound to the type ' MyModel

', which in the view will allow you to do:



var myDataInTheView = model.data1;

      

Then you can use the foreach approach as described by others, but about the properties of the model and not about the model class itself. In the above snippet the loopmyDataInTheView

+1


source







All Articles