Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7693505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:06:16+00:00 2026-05-31T21:06:16+00:00

How do I put a dataset into a viewbag and display the result in

  • 0

How do I put a dataset into a viewbag and display the result in a view?

I have a dataset from a model that I write to a viewbag. I want to use a foreach loop to get the datarows out of the viewbag in the view.

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

model

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

controller:

 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"];
        } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:06:18+00:00Added an answer on May 31, 2026 at 9:06 pm

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

    The View will have a Model property that represents an instance of the class type specified by your @Model declaration. In your code, you are using the controller class which is not going to work. I rewrote the example to use a DataSet as the model. As you can see, the Model property of the View becomes an instance of the System.Data.DataSet class 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 is an example that uses a Dictionary in the model class to store multiple DataSets. You can then modify the View to use the modeldata type as its 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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simply put, MVC is the pattern for separating contents (model) from presentation (view), and
I have created a script that reads from a CSV (or other dataset, but
I have a dataset from a complex query which I've selected into a temp
I have a dataset of values, which I need to put into a textbox.
Put it another way: what code have you written that cannot fail. I'm interested
I have a dataset and I write it's values to an excel file using
I have a report that takes one parameter, and works fine. So I've put
I have the following code that loads an XML file into a datatable (I
I get a DataTable from a DataSet and then bind that DataTable to a
Im trying to retrieve a datatable that I put into a session variable, however

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.