Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Move list of Data to View MVC

i use this code to git Data fro DB in model public string DBconect = ConfigurationManager.ConnectionStrings["DB"].ConnectionString; public void add() { using (SqlConnection con = new SqlConnection(DBconect)) {

            SqlCommand cmd = new SqlCommand("getRoom_empty", con);
            cmd.CommandType = CommandType.StoredProcedure;
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {

            }

        }
    }

how to move data to View Page

1 Answer

Jon Wood
Jon Wood
9,884 Points

If you're not sure how to get data from the SqlReader, check this StackOverflow post.

I don't know how the rest of your project looks, but if you want to use the data in a view, you could map the data to a model class and use that in your view. Typically, you'd call data methods from the controller via a repository and use the results from there to pass into your view.

Hope that helps, but let me know if you need more details.

i know how use datareader but i ask to move Dataset from Model To View Page

Jon Wood
Jon Wood
9,884 Points

Right, just pass the model into the view, like this - View(model). You can strongly type the model on the view page with @model ModelType.