ASP.NET MVC View with embedded code displays html out of sequence - asp.net

I have a view in ASP.NET MVC. It takes the model object and iterates over a list of strings and displays them in a table row, like so:
Details
<table>
<tbody>
<tr>
<th>Values in the database</th>
</tr>
<% foreach (string value in Model.lstDistinctValues)
{%>
<tr>
<%=value%>
<%} %>
</tr>
</tbody>
The problem is that the values appear ABOVE the header. So 'Values in the database' appears at the bottom, while the values are at the top.
Why would this be happening?

You need to add TD elements.
<table>
<tbody>
<tr>
<th>Values in the database</th>
</tr>
<% foreach (string value in Model.lstDistinctValues)
{%>
<tr>
<td><%=value%></td>
</tr>
<%} %>
</tbody>
</table>

Related

How to convert a SQL query to a multidimensional hash?

I am wishing to have a multidimensional hash, given a query which joins 4 relevant tables.
Mainly I want this to serve a screen where I have three levels of information. When I click Process, I get a table with activities, and when clicking activities, get a table with both pollution_origins and sub_pollution_origins.
So, I have 5 models, where one of them is a relational one (process_model_maps), which connects the other four.
There is an hierarchy between them (sub_pollution_origin_models are inside pollution_origin_models, which are inside of activity_models, which are part of at least one process_model)
Currently I have this:
_process_models_maps_list.html.erb
<table id="all_processes">
<tbody>
<% #process_models.each do |process_model| %>
<tr class="process_line_color"> <td><%= process_model.name %></td> </tr>
<tr>
<%= render partial: "process_activities",
locals: { process_model: process_model} %>
</tr>
<% end %>
</tbody>
</table>
_process_activities.html.erb
<table id = "process_activities">
<tbody>
<% process_model.activity_models.each do |activity_model| %>
<tr class="activity_line_color"> <td><%= activity_model.name %></td> </tr>
<tr>
<%= render partial: "activity_sub_pollution_origin_models",
locals: { activity_model: activity_model} %>
</tr> <!-- row activity_model details-->
<% end %>
</tbody>
</table>
_activity_sub_pollution_origin_models.html.erb
<table id = "activity_details">
<tbody>
<% activity_model.process_model_maps.each do |process_model_map| %>
<tr>
<td> <%= process_model_map.pollution_origin_model.name %></td>
<td> <%= process_model_map.sub_pollution_origin_model.name %> </td>
</tr>
<% end %>
</tbody>
</table>
The issue is that if I just use regular activerecord, rails goes on and sets a great number of queries, which kills performance for the screen.
So I thought that if I had a pre-loaded hash somehow, this would make the task easier for the end user.
in my ProcessModel.rb I have
def self.get_tree_map
joins(process_model_maps: [:activity_model, :pollution_origin_model, :sub_pollution_origin_model]).select("process_models.id, process_models.name, activity_models.id,activity_models.name, process_model_maps.id , pollution_origin_models.name,sub_pollution_origin_models.name")
end
and am now thinking on getting something like:
{Project_id: 1, name: Fundicao, {activity_id:1, name: Decapagem,{process_model_maps_id: 2, pollution_origin_models_id: 1, pollution_origin_models_name: " Energia ", sub_pollution_origin_models_id: 1, sub_pollution_origin_models_name: "Energia Eletrica" },...}}}
In your controller:
#process_models = Process.all.joins(:activities).merge(
Activity.joins(:pollution_origins))
That should fetch all of the data you want with a single SQL call.

How to Sum from query search result

I want to SUM from a query search result. I have a table called Students. I created a Search form to list students with scores from Jan or Feb, by date range. The results are different (two rows, four rows, so on) based on how many months I search. I created a Sum row at the bottom to count the total from that column for Jan or Feb. However, it doesn't work at all.
Can anyone help?
Here is my code:
<table>
<tr>
<td>Student Name</td>
<td>Jan</td>
<td>Feb</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><%=oRs("Name")%></td>
<td <%=sRowStyle%>><%=oRs("Jan")%></td>
<td <%=sRowStyle%>><%=oRs("Feb")%></td>
</tr>
<tr>
<td colspan="3"><% total = total + CInt(oRs("Jan")) oRs.MoveNext WEND %>
</td>
<tr>
</tr>
</table>
It looks like you haven't defined where your recordset loop starts
Try this:
<% Dim CountJan, CountFeb
Do while not oRS.eof %>
<tr>
<td><%=oRs("Name")%></td>
<td <%=sRowStyle%>><%=oRs("Jan")%></td>
<td <%=sRowStyle%>><%=oRs("Feb")%></td>
</tr>
<% CountJan = CountJan + Cint(oRs("Jan"))
CountFeb = CountFeb + Cint(oRs("Feb"))
oRs.MoveNext
Loop
%>
<tr>
<td> </td>
<td><%=CountJan%></td>
<td><%=CountFeb%></td>
</tr>
You can use while and wend instead of do while not and loop, its a matter of personal preference

Loop based in variable value in asp classic

I have to output a table based on value of a variable. I have a unique block of table with variables. Depending on the view there should be a loop the re-read the table.
The variable could have the values Two or Three or both. I have to output the table with view One in all the cases.
Example #1:
Views = "Two, Three"
Output:
<table>
<tr>
<td>Table For View One</td>
</tr>
</table>
<table>
<tr>
<td>Table For View Two</td>
</tr>
</table>
<table>
<tr>
<td>Table For View Three</td>
</tr>
</table>
Example #2:
Views = "Two"
Output:
<table>
<tr>
<td>Table For View One</td>
</tr>
</table>
<table>
<tr>
<td>Table For View Two</td>
</tr>
</table>
Example #3:
Views = "Three"
Output:
<table>
<tr>
<td>Table For View One</td>
</tr>
</table>
<table>
<tr>
<td>Table For View Three</td>
</tr>
</table>
Tried to mix some While and For, without success.
<%
i=2
Do While i = 0
%>
<table>
<tr>
<td><% Response.Write i %></td>
</tr>
</table>
<%
i=i-1
Loop
%>
Try this:
<%
Dim Views : Views ="Two" 'Change this to check
Response.write "Initial View =" & Views & "<br/>"
'But "One" will always be printed
If InStr(Views,"One")=0 Then
'add if there is no "One" already
Views = Views & ",One"
End If
Response.write "New View =" & Views & "<br/>"
Dim ArrValues : ArrValues =Array("One","Two","Three")
Dim Counter
For Counter=0 to UBound(ArrValues)
If InStr(Views,ArrValues(Counter))<>0 Then
%>
<table>
<tr>
<td>Table For View <%=ArrValues(Counter)%></td>
</tr>
</table>
<%
'Response.write ArrValues(Counter) & "<br/>"
End If
Next
%>
I think something like this would work fine. I just use if else if. Also I compansated for the case that Views could equal "Two, Three" or "Three, Two".
<%if Views = "Two" then%>
<table>
<tr>
<td>Table For View One</td>
</tr>
</table>
<table>
<tr>
<td>Table For View Two</td>
</tr>
</table>
<%else if Views = "Three"%>
<table>
<tr>
<td>Table For View One</td>
</tr>
</table>
<table>
<tr>
<td>Table For View Three</td>
</tr>
</table>
<%else if Views = "Two, Three"%>
<table>
<tr>
<td>Table For View One</td>
</tr>
</table>
<table>
<tr>
<td>Table For View Two</td>
</tr>
</table>
<table>
<tr>
<td>Table For View Three</td>
</tr>
</table>
<%else if Views = "Three, Two"%>
<table>
<tr>
<td>Table For View One</td>
</tr>
</table>
<table>
<tr>
<td>Table For View Three</td>
</tr>
</table>
<table>
<tr>
<td>Table For View Two</td>
</tr>
</table>
<%end if%>
There are multiple ways to approach this but without changing your code to much, it can be done by splitting your Views variable into an Array using the comma , as a delimiter. Once you have the Array use a For loop to iterate through the Views and use a Select statement to return the correct view.
As you specified that Table One will always be included it has been left outside of the loop.
Something like this (not tested);
<%
Dim item, items
'Build the array from the Views comma separated string
Views = Split(Views, ",")
'Always include Table One
%>
<table>
<tr>
<td>Table For View One</td>
</tr>
</table>
<%
'Check we have an Array built from the Split().
If IsArray(Views) Then
'How many views have we requested?
items = UBound(Views)
For item = 0 To items
'What view are we current looking at in the loop?
Select Case LCase(Trim(Views(item) & ""))
Case "two"
%>
<table>
<tr>
<td>Table For View Two</td>
</tr>
</table>
<%
Case "three"
%>
<table>
<tr>
<td>Table For View Three</td>
</tr>
</table>
<%
End Select
Next
End If
%>
Depending on the complexity of your views you could break each table into it's own Sub procedure or one Sub procedure and call in place of the in-line table definitions. Something like below;
Sub ShowTable(view)
'Keep all in-line table definitions together.
Select Case Trim(LCase(view & ""))
Case "one"
%>
<table>
<tr>
<td>Table For View One</td>
</tr>
</table>
<%
Case "two"
%>
<table>
<tr>
<td>Table For View Two</td>
</tr>
</table>
<%
Case "three"
%>
<table>
<tr>
<td>Table For View Three</td>
</tr>
</table>
<%
End Select
End Sub
In the main loop you would added ShowTable() calls in place of any in-line table code, for example;
<%
Dim item, items
'Build the array from the Views comma separated string
Views = Split(Views, ",")
'Always include Table One
Call ShowTable("one")
'Check we have an Array built from the Split().
If IsArray(Views) Then
'How many views have we requested?
items = UBound(Views)
For item = 0 To items
'What view are we current looking at in the loop?
Call ShowTable(views(item))
Next
End If
%>
Your output will then look something like this (if you pass "Two, Three") for example;
<table>
<tr>
<td>Table For View One</td>
</tr>
</table>
<table>
<tr>
<td>Table For View Two</td>
</tr>
</table>
<table>
<tr>
<td>Table For View Three</td>
</tr>
</table>
Update based on OP feedback
If you don't want multiple tables (which is not clear from your question) then just remove the <table> and </table> in-line definitions (in the ShowTable() procedure), this will create the "guts" of your table (a series of table rows).
Then in the example code above just add <table> at the start and after the code </table>, which will encapsulate your dynamically generated table rows into one table.
<table>
<%
Dim item, items
'Build the array from the Views comma separated string
Views = Split(Views, ",")
'Always include Table One
Call ShowTable("one")
'Check we have an Array built from the Split().
If IsArray(Views) Then
'How many views have we requested?
items = UBound(Views)
For item = 0 To items
'What view are we current looking at in the loop?
Call ShowTable(views(item))
Next
End If
%>
</table>
Your output will then look something like this (if you pass "Two, Three") for example;
<table>
<tr>
<td>Table For View One</td>
</tr>
<tr>
<td>Table For View Two</td>
</tr>
<tr>
<td>Table For View Three</td>
</tr>
</table>

How to make the table 2 * 2 with rules in aspx page

Can I make the table with rules in my aspx page.
right now I am dispalying the page some thing like this.
Benefit Type : 000
Benfit Set: BCPCP
Converage Level : IND -Individual
Deductable Type : D-DED
Can I differnciate with the rules each and every row. like this
Benefit Type : 000
Benfit Set: BCPCP
Converage Level : IND -Individual
Deductable Type : D-DED
here is my Aspx code.. is there any way I can make like this?
<table>
<tr>
<td>
</td>
</tr>
You can just do a row with an hr in it like this: http://jsfiddle.net/WXSpD/
<table>
<tr>
<td>Benefit Type:</td>
<td>000</td>
</tr>
<tr>
<td colspan="2"><hr/></td>
</tr>
<tr>
<td>Benfit Set:</td>
<td>BCPCP</td>
</tr>
</table>
Your question is very hard to understand, but if you're just looking for a two-by-two table, can't you do it like this?:
<table>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
If there's something else you're looking for, can you clarify a little bit?

Possible to dynamically add an asp:CheckBox control to a TableCell?

In my .NET application I need to add a checkbox to each row in a dynamically created asp:Table.
Is it possible to do that by dynamically creating an asp:CheckBox for each row and somehow put it inside a TableCell object? (In that case how?)
Or do I need to replace the asp:table control with something else, like a Repeater control or GridView to make it work?
I'm looking for the quickest solution because I don't have much time.
Thanks in advance!
/Ylva
in aspx:
<asp:Table id=T1 runat=server />
in cs:
TableCell tc;
foreach(TableRow tr in T1.Rows)
{
tr.Cells.Add(tc = new TableCell());
((IParserAccessor)tc).AddParsedSubObject(new CheckBox());
}
You do not want to do it on server side( in the cs as Yossarian said). because every time your page is reloaded or refreshed, you would have to recreate those checkboxes, which would mean new checkboxes every load, which also would mean your checkbox controls info will be lost because they are not on the client side, so all updated info done by the user (checkbox checked)will be lost, so you want be able to find out what is checked unless you add jquery in and it starts to get more complicated then it needs to be
if you are using webpages then it would be best to use asp:Gridview web control and bind the data to the table in code behind as so:
Gridview.Datasource=//ex:data;
Gridview.Databind();
As shown in the example on this page here
but if you are using MVC then you would add them in the client code in a form as so:
<% using (Html.BeginForm("Presentation", "Home")) %>
<% { %>
<table id="Table" class="color" width="100%" border="1">
<colgroup width="3%" ></colgroup>
<colgroup width="15%"></colgroup>
<colgroup width="20%"></colgroup>
<colgroup width="15%"></colgroup>
<colgroup width="47%"></colgroup>
<thead>
<tr class="dxgvHeader_Glass">
<th id="CheckBox" class="style1" ><input type="checkbox" class="selectall" id="selectall" name="CheckBox" /></th>
<th id="DateTime" runat="server"></th>
<th id="Description" runat="server"></th>
</tr>
</thead>
<tbody >
<%try
{ %>
<% foreach (var SamAuditLog in ViewData.Model)
{ %>
<tr>
<td class="style1" align="center"><%=Html.CheckBox(""+data.ID) %></td>
<td><%= data.DateTime%></td>
<td><%= data.Description%></td>
</tr>
<% } %>
<%} %>
</tbody>

Resources