How do I display a datatable in asp.net mvc 2? - c#

I am running a dynamically built SQL statement and putting the results in a datatable. I then need to display these results as a table in a partial view. I would normally create a List<> object and strongly type it to the page, but in this case I do not know what the final sql will be, since it is built by the user at run time.
So, how can I display the data in a datatable when I do not know what is in it? Also is there a better way to do this than in a datatable?
Thanks

As you are displaying tablular data, you should use an html table to do the job. Dismissile's answer only displays one column of the table. The more generic answer can be found in the following SO question:
Displaying standard DataTables in MVC
The bit that concerns you is, with the model strongly typed as a 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>
EDIT
Edited to encode the content of the datable. As using mvc 2 (according to the tag), then Html.Encode not required, just the <%: notation that is available in mvc 2.

Related

How to insert different images in ASP.NET MVC while using a foreachloop and a db

<table class ="table" cellpadding="0" cellspacing="0">
<tr>
<th>Item Name</th>
<th>Price</th>
<th>Seafood</th>
<th>Has Gluten</th>
<th>Picture</th>
</tr>
#foreach (var items in #Model)
{
<tr>
<td>#items.ItemName</td>
<td>#items.Price</td>
<td>#items.IsSeafood</td>
<td>#items.HasGluten</td>
</tr>
}
I am currently learning how to develop using ASP.NET MVC.
I am trying to insert different images for each of my items. My foreach loop creates the table and items from a mysql database. I want to add the pictures next to the tables unless I'm unable to with the code I have.
You can use an <img src=""/> tag inside your last <td> to embed the image directly into the html.
Something like this, depending on how your image is stored.
<td><img src="#items.Url"/></td>

Single view for all the Grids in MVC

I have a requirement like display many of collections in a HTML table format like mentioned below.
<table>
<thead>
<tr>
<th>Column 1 Heading</th>
<!--More column headings-->
</tr>
</thead>
<tbody>
#foreach (var item in Model.MyCollection)
{
<tr>
<td>#item.PropertyName</td>
<!--More properties-->
</tr>
}
</tbody>
<!--Add footer for totals-->
</table>
I will not specify the view model property names explicitly. I wanted to pass any type of collections to view that will need to generate a HTML table for me.
Please suggest better way of doing it.

Get client side HTML changes on postback

I want to add rows to a table with javascript, but I also want to be able to find out what those rows are on postback. Is there a way to do that?
I also want to be able to populate the original rows in the table from the server (I'm thinking with a repeater). Is it still possible to do that?
That's not much of a description but I think that covers it...
The code currently looks something like this
<table id="myTable">
<tr> <td> some static row </td> </tr>
<asp:repeater id="rptTest" runat="server">
<HeaderTemplate>
<tr class="dgheader">
<th> head1 </th>
<th> head2 </th>
<th></th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class="<%# (Container.ItemIndex%2 == 0) ? "dgitem" : "dgalternatingitem" %>">
<td><%# Eval("val1") %> </td>
<td><%# Eval("val2") %> </td>
<td><a class="dgdeletebutton" href="javascript:delete(this)"></a></td>
</tr>
</ItemTemplate>
</asp:repeater>
</table>
At the moment all I'm wondering is how, server side, I can get a version of the table that has whatever changes I made client side.
In order to get any information from a client in the manner you describe, you need to include a field in your form submit.
You will probably want hidden(s) field. Any time you add a row, either add a hidden field for each value you want to capture (such as val1 and val2) or have one hidden field, and when you add a row, append the information you want to the existing row.
I would warn against posting straight html, you probably only need the values not the full markup, and you most likely don't want to sanitize the html and parse it for the information you want.
So to get you a head start you can add hidden inputs:
<tr class="<%# (Container.ItemIndex%2 == 0) ? "dgitem" : "dgalternatingitem" %>">
<input type="hidden" name="Row[1].val1" value="myvalue" />
<td><%# Eval("val1") %> </td>
<input type="hidden" name="Row[1].val2" value="myvalue" />
<td><%# Eval("val2") %> </td>
<td><a class="dgdeletebutton" href="javascript:delete(this)"></a></td>
</tr>
You can then get the submitted values on the backend:
HttpContext.Current.Request.Form["Row[1].val1"]
This is from memory, the line above might not be correct.

How to fill IEnumerable<T> after HttpPost request mvc 3?

i want to make a form using below code i developed Model for View :
Please read the following blog post to better understand how the model binding works for collections and how your input fields should be named: http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx
OK, now let's get rid of this foreach loop and use editor templates, shall we?
<table style="width:65%; vertical-align:top" id="sample">
<%= Html.EditorFor(x => x.Properties) %>
</table>
and now define an editor template that will automatically be rendered for each element of the properties collection (~/Views/Shared/EditorTemplates/PropertyModel.ascx):
<%# Control
Language="C#"
AutoEventWireup="true"
Inherits="System.Web.Mvc.ViewUserControl<PropertyModel>"
%>
<tr>
<td>
<%= Html.LabelFor(x => x.ParameterName) %>
</td>
<td>:</td>
<td>
<%= Html.TextBoxFor(x => x.ParameterName) %>
</td>
</tr>
As far as those radio buttons are concerned, there's something wrong in your design about them. They are not part of the collection model but part of the main view model and yet you are putting them inside the foreach loop that is rendered for each element of the collection property. You might need to rethink what you are trying to do here.

Display data tables grouped and separated by headings on an MVC view

I'm a complete C# AND MVC noob, so this one's been a bitter struggle for me.
I've done a horrible but ultimately successful job of building a website for work to display the results of the impending primary/local elections.
What I need in the final view of my site is to display all the results of races in the same category. For example, there are three city commission positions open, and I need results for all three races on one page. I can display all the results I want, filtered by category, and in the pretty css table, what I can't seem to figure out is how I split that table up with sub headings for each of the individual races.
The data is direct Linq to SQL and comes out of a single view on the server (repositories are a thing that will have to happen in my next project unless they are essential to this function).
I'm not quite sure if this is what you are after, do let me know and I'll try to help.
This won't work directly for you, but hopefully might point you in the right way. I'm currently using a custom view model to hold this data, which I'm actually thinking of moving away from as my system is getting very complicated, but it might help you.
I can explain what that means if you need me to, (I was a noob just a year ago!)
I have assumed that you want races grouped by category.
<% foreach (var group in Model.GroupBy(item => item.categoryId)) Gives you the inital sort
{ %>
<% foreach (var item in group.Take(1))
{ //Category %>
<%=Html.Encode(item.CategoryName) %>
<% } %>
<% foreach (var item in group)
{ //Indervidual races%>
<%=Html.Encode(item.raceResult) %>
<% } %>
<% foreach (var item in group.Take(1))
{ %>
<!-- Area which happens after each category grouping -->
<% } %>
<% } %>
THANK YOU!! That was precisely the information I was looking for. Model.groupby was the break through for me.
Here's the code I used in the final FWIW:
<%
foreach (var group in Model.GroupBy(item => item.contestNumber))
{
foreach (var item in group.Take(1))
{
%>
<table width="600">
<tr>
<th>
<%: item.txtContest %>
</th>
<th width="40"></th>
</tr>
<%
foreach (var result in group)
{
%>
<tr>
<td>
<%: result.Candidate %>
</td>
<td>
<%: result.voteTotal %>
</td>
</tr>
<%
}
%>
</table>
<br />
<br />
<br />
<%
}
}
%>

Categories

Resources