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 622149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:57:11+00:00 2026-05-13T18:57:11+00:00

Perhaps this is just completely wrong, but back in the days of Webforms you

  • 0

Perhaps this is just completely wrong, but back in the days of Webforms you would return a Dataset which you would then bind to a grid. But now in MVC you’re not supposed to pass a datatable because you cannot serialize it and it’s technically passing objects into the View where it doesn’t belong? But how on earth am I meant to display data on a view?! I can’t use LINQ to SQL classes here since this is a pure in memory data structure.

Ideally I’d just like to able to have an object which I can iterate within the view.

I’m really at a bit of a loss I have read the article from the “Gu” and I can only summarize that I have to pass back a ViewData Object instead?? Am I going nuts here?

Cheers from Blighty

Jon

  • 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-13T18:57:12+00:00Added an answer on May 13, 2026 at 6:57 pm

    This is not “wrong” at all, it’s just not what the cool guys typically do with MVC. As an aside, I wish some of the early demos of ASP.NET MVC didn’t try to cram in Linq-to-Sql at the same time. It’s pretty awesome and well suited for MVC, sure, but it’s not required. There is nothing about MVC that prevents you from using ADO.NET. For example:

    Controller action:

    public ActionResult Index()
    {
        ViewData["Message"] = "Welcome to ASP.NET MVC!";
    
        DataTable dt = new DataTable("MyTable");
        dt.Columns.Add(new DataColumn("Col1", typeof(string)));
        dt.Columns.Add(new DataColumn("Col2", typeof(string)));
        dt.Columns.Add(new DataColumn("Col3", typeof(string)));
    
        for (int i = 0; i < 3; i++)
        {
            DataRow row = dt.NewRow();
            row["Col1"] = "col 1, row " + i;
            row["Col2"] = "col 2, row " + i;
            row["Col3"] = "col 3, row " + i;
            dt.Rows.Add(row);
        }
    
        return View(dt); //passing the DataTable as my Model
    }
    

    View: (w/ Model strongly typed as System.Data.DataTable)

    <table border="1">
        <thead>
            <tr>
                <%foreach (System.Data.DataColumn col in Model.Columns) { %>
                    <th><%=col.Caption %></th>
                <%} %>
            </tr>
        </thead>
        <tbody>
        <% foreach(System.Data.DataRow row in Model.Rows) { %>
            <tr>
                <% foreach (var cell in row.ItemArray) {%>
                    <td><%=cell.ToString() %></td>
                <%} %>
            </tr>
        <%} %>         
        </tbody>
    </table>
    

    Now, I’m violating a whole lot of principles and “best-practices” of ASP.NET MVC here, so please understand this is just a simple demonstration. The code creating the DataTable should reside somewhere outside of the controller, and the code in the View might be better isolated to a partial, or html helper, to name a few ways you should do things.

    You absolutely are supposed to pass objects to the View, if the view is supposed to present them. (Separation of concerns dictates the view shouldn’t be responsible for creating them.) In this case I passed the DataTable as the actual view Model, but you could just as well have put it in ViewData collection. Alternatively you might make a specific IndexViewModel class that contains the DataTable and other objects, such as the welcome message.

    I hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is just out of interest, and perhaps could be useful for my
I probably just haven't thought this through, or perhaps I'm simply unaware of an
Perhaps this question has been asked elsewhere, but I'm unable to find it. With
Perhaps this has something to do with it being the mainForm, but I'll ask
I have an ObjectDataSource (but perhaps this question is the same for all kinds
I'm new to visual studio, so perhaps this is something easy... but I've had
This is perhaps more of a discussion question, but I thought stackoverflow could be
OK, this is perhaps stupidest question ever but bear with me.... How to make
Perhaps properties aren't the way to go with this, but I'm struggling to find
Perhaps I'm not understanding completely, but I'm trying to get the {@link} inline PHPDoc

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.