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
After adding Script and CSS reference next we are going to add DataTables Markup. It is simple Html Table in that we are going to add columns headers (“<th>”) with all the columns names which we want to display on the grid. After adding Markup next, we are going to add DataTables function to Create DataTables.
A DataView enables you to create different views of the data stored in a DataTable, a capability that is often used in data-binding applications. Using a DataView, you can expose the data in a table with different sort orders, and you can filter the data by row state or based on a filter expression.
In the ADO.NET library, C# DataTable is a central object. It represents the database tables that provide a collection of rows and columns in grid form. There are different ways to create rows and columns in the DataTable.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With