Dynamic Table alternate background - MVC3 - css

I have two tables in my view; trying to change the background of alternate rows in one table. In my style file I am using:
.alternateRow
{
table-layout: auto;
border-collapse: collapse;
border-spacing: 0px;
empty-cells: hide;
}
.alternateRow tr {
background-color:#aa0000;
}
.alternateRow tr.odd {
background-color:#00AA00;
}
This is how I make the dynamic table:
<table class="alternateRow">
<% List<Question> wrongs = ViewBag.wrongs;
for (var i = 0; i < wrongs.Count; ++i)
{ %>
<tr>
<td>
<%= wrongs[i].TheQuestion %>
</td>
<td>
Correct Answer
</td>
<td>
<%= wrongs[i].CorrectAnswer %>
</td>
<td>
<%= wrongs[i].Explanations %>
</td>
</tr>
<% } %>
I did not work, both rows have the same color. Any idea what I am doing wrong?

Here is what you can do:
<% List<Question> wrongs = ViewBag.wrongs;
for (var i = 0; i < wrongs.Count; ++i)
{
var rowClass = ((i % 2) == 0 ? "" : "odd"); // or whatever your alternate row class is
%>
<tr class="<%= rowClass %>">
<td>
<%= wrongs[i].TheQuestion %>
</td>
<td>
Correct Answer
</td>
<td>
<%= wrongs[i].CorrectAnswer %>
</td>
<td>
<%= wrongs[i].Explanations %>
</td>
</tr>
<% } %>

Related

Create and delete tables in responsive design, Ruby on Rails 4

I want that, depending on the width of the browser, tables should be created or deleted.
In CSS, you can use something like: `#media (min-width: 767px), whereas css-settings change, after width passes 767px. Is there something similar for ruby on rails? For example:
In my book/view.html.erb there is a table like this:
<table>
<tr>
<th>Title</th>
<th>Genre</th>
<th>Description</th>
</tr>
<tr>
<td><%= #book.title %></td>
<td><%= #book.genre %></td>
<td><%= #book.desc %></td>
</tr>
</table>
What I want is, that, after width gets smaller than 767px, it should look like this:
Title: <br>
<%= #book.title %><br>
Genre: <br>
<%= #book.genre %>
Description:
<%= #book.desc %>
How can I accomplish this? Thanks in advance!
Is there something similar for ruby on rails
Yes, it's called browser - a gem:
[Browser] adds a helper method called browser, that inspects your current user agent:
#app/views/controller/your_view.html.erb
<% if browser.tablet? || browser.mobile? %>
... do something ...
<% end %>
--
The problem with this, however, is that it only works when you render the page.
CSS Media queries adapt when the page changes dimension, meaning that if you decide to resize the app on your desktop, the CSS will immediately adapt.
Rails compiles code based on an HTTP request, meaning that it cannot "change" as CSS does. Whilst this shouldn't be a problem for tablets etc, it may cause issues for desktops.
Solution
What I want is, that, after width gets smaller than 767px, it should look like this
<% if browser.mobile? %>
Title: <br>
<%= #book.title %><br>
Genre: <br>
<%= #book.genre %>
Description:
<%= #book.desc %>
<% else %>
<table>
<tr>
<th>Title</th>
<th>Genre</th>
<th>Description</th>
</tr>
<tr>
<td><%= #book.title %></td>
<td><%= #book.genre %></td>
<td><%= #book.desc %></td>
</tr>
</table>
<% end %>
A completely CSS solution (highly recommended):
#app/assets/stylesheets/application.css
#media screen and (max-width: 767px) {
.big { display: none; }
.small { display: block; }
}
#app/view
<%= content_tag :div, class: "big" do %>
<table>
<tr>
<th>Title</th>
<th>Genre</th>
<th>Description</th>
</tr>
<tr>
<td><%= #book.title %></td>
<td><%= #book.genre %></td>
<td><%= #book.desc %></td>
</tr>
</table>
<% end %>
<%= content_tag :div, class: "small" do %>
Title: <br>
<%= #book.title %><br>
Genre: <br>
<%= #book.genre %>
Description:
<%= #book.desc %>
<% end %>

rails css How to place two button in a line

I'm rails beginner.
I want to make two button that are placed in a line.
How can I put the button correctly on a line of the table?
Two buttons:
<%= button_to "empty Cart", #cart, method: :delete %>
<%= submit_tag "Delete item" %>
my view
<%= form_tag destroy_multiple_carts_path, method: :delete do %>
<table class="table">
<tbody>
<tr>
<td class = "checkbox_size_sm"><input type="checkbox"></td>
<td class = "image">ITEM</td>
<td class = "title"> </td>
<td>PRICE</td>
</tr>
<% #cart.line_items.order(created_at: :desc).each do |item| %>
<tr>
<td><%= check_box_tag "item_ids[]", item.id %></td>
<td class = "image"><%= image_tag item.product.item, size: "100" %>
</td>
<td class = "title" id = "td_align_middle">
<%= item.product.title %><br>
<%= item.product.brand %>
</td>
<td id = "td_align_middle"><%= item.product.price %></td>
</tr>
<% end %>
</tbody>
<tfoot>
<tr>
<td colspan = "4" class="total_price" >
Total Price <strong> <%= #cart.cart_total_price %> </strong>
</td>
</tr>
<tr>
<td colspan ="4" class="al-r">
<%= button_to "empty Cart", #cart, method: :delete %> <%= submit_tag "Delete item" %>
</td>
</tr>
</tfoot>
</table>
</div>
</div>
Help me..
wrap those buttons in a div, set the div to float left and overflow hidden
add hose two buttons in div with class divname and in css file add below style to
div.divname button { float:left; margin-right:10px; }

Html.textboxfor showing email address in chrome where as model has empty string

I have an asp.net mvc which gets model data from control action. I see that model.genericpassword field is empty string but in view it shows an email address ( in chrome only). This email address comes from the login page which is before this view. Is it cache issue or what ? If I use Html.passwordfor, then it shows empty textbox ( which is what I want). But I want to show password in plain format using html.textboxfor. This issue is not in Firefox or IE. If model has value then it shows correct value.
Controller action is
[Authorize]
public ActionResult EditMyUserDetails()
{
// IMS.ManagerObjects.IUserProfileManager mgr = profilemanager; //Roopa 29 Oct 2012
using (IUserProfileManager profilemanager = new ManagerFactory().GetUserProfileManager())
{ //Roopa 29 Oct 2012
UserProfile profile = profilemanager.GetProfile(CurrentUser.Id, CurrentAccount.Id);
UserModel model = userauthenticationmanager.GetModelWithProfileInfo(CurrentUser, profile);
model.PasswordReminder = model.PasswordReminder != null ? model.PasswordReminder : "";
model.GenericPassword = model.GenericPassword != null ? model.GenericPassword : "";
string[] userTypes = new string[] { "Admin", "Standard User" };
ViewData["userTypes"] = new SelectList(userTypes, model.myRole.RoleName);
using (ICurrencyManager currencyManager = new ManagerFactory().GetCurrencyManager())
{
List<Currency> currencies = currencyManager.GetAll().ToList();
int currencyId = CurrentAccount.Currency.Id;
Currency currency = currencyManager.GetById(currencyId);
ViewData["currencies"] = new SelectList(currencies, "DialPrefix", "CountryDialPrefix", currency.DialPrefix);
}
return View(model);
}
}
and view is:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/child.Master" Inherits="System.Web.Mvc.ViewPage<IMS.Model.UserModel>" %>
<asp:Content ID="contentHead" ContentPlaceHolderID="PageHead" runat="server">
<script src="../../Content/custom-js/EditMyUserDetails.js" type="text/javascript"></script>
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContainer" runat="server">
<script type="text/javascript">
$(document).ready(function () {
$("#frmEditMyDetails :input").not(":button").change(function () {
IsChanged(true);
});
$("#dialog-confirm").dialog({
autoOpen: false,
resizable: true,
modal: true,
buttons: {
"Yes": function () {
save();
},
No: function () {
document.location.href = $(this).data('Parameter').address;
$(this).dialog("close");
}
}
});
});
</script>
<style type="text/css">
.wide
{
width: 400px;
}
.disabled
{
background-color: #ebebe4;
}
.widetextarea
{
width: 400px;
height: 100px;
}
.normal
{
width: 317px;
}
.small
{
width: 40px;
}
.tiny
{
width: 25px;
}
</style>
<div id="dialog-confirm" title="Do you want to save changes?" style="display: none;">
<p>
<span class="ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>Changes
made to user details are not saved.Do you want to save ?</p>
</div>
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm("EditMyUserDetails", "Account", FormMethod.Post, new { id = "frmEditMyDetails", name = "frmEditMyDetails" }))
{ %>
<%= Html.ValidationSummary(true)%>
<%= Html.HiddenFor(model => model.id)%>
<table>
<tr>
<td style="width: 710px;">
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td>
Email :
</td>
<td style="padding-left: 1px">
<%= Html.TextBoxFor(model => model.Email, new { #class = "textarea", #disabled ="disabled", #readonly = "readonly", MaxLength = "50" })%>
</td>
</tr>
<tr>
<td>
Landline :
</td>
<td style="padding-left: 1px">
<%= Html.TextBoxFor(model => model.Landline, new { #class = "textarea", MaxLength = "50" })%>
</td>
</tr>
<tr>
<td>
Mobile :
</td>
<td style="padding-left: 1px">
<%= Html.TextBoxFor(model => model.Mobile, new { #class = "textarea", MaxLength = "50" })%>
</td>
</tr>
<tr>
<td>
Generic Password :
</td>
<td style="padding-left: 1px">
<%= Html.TextBoxFor(model => model.GenericPassword, new { #class = "textarea editable", MaxLength = "50" })%>
</td>
</tr>
<tr>
<td>
Add generic password by default :
</td>
<td style="padding-left: 0px">
<%-- <%= Html.CheckBox("defaultGeneric")%>--%>
<%= Html.CheckBoxFor(model => model.IsGenericDefault )%>
</td>
</tr>
<tr>
<td>
Prompt for unique password by default :
</td>
<td style="padding-left: 0px">
<%--<%= Html.CheckBox("PromptForPass")%>--%>
<%= Html.CheckBoxFor(model => model.IsUniqueDefault )%>
</td>
</tr>
<%-- <tr>
<td>
Do not add a password by default :
</td>
<td style="padding-left: 0px">
<%= Html.CheckBox("Defaultpass")%>
</td>
</tr>--%>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td>
Login Password :
</td>
<td>
<%=Html.PasswordFor(model => model.Password, new { #class = "textarea editable" })%>
<%=Html.HiddenFor(model => model.HiddenPassword)%>
</td>
</tr>
<tr>
<td>
New Password :
</td>
<td>
<%=Html.PasswordFor(model => model.NewPassword, new { #class = "textarea editable" })%>
</td>
</tr>
<tr>
<td>
Confirm Password :
</td>
<td>
<%=Html.PasswordFor(model => model.ConfirmPassword, new { #class = "textarea editable" })%>
</td>
</tr>
<tr style="height: 10px;">
<td colspan="2">
</td>
</tr>
<tr>
<td colspan="2">
<b>Please enter some text which will help you remember your password</b>
</td>
</tr>
<tr>
<td>
Password Reminder :
</td>
<td>
<%=Html.TextBoxFor(model => model.PasswordReminder, new { #class = "textarea editable" })%>
</td>
</tr>
</table>
</td>
</tr>
</table>
<% } %>
</asp:Content>
There are other textboxes on this page which work fine.

asp.net mvc3 razor syntax convert

How do I convert this gridview in razor syntax, I have curly brackets inside (data => { %>) ?
<%Html.GridView<Employee>(
Model,
data => { %>
<table class="grid" cellpadding="0" cellspacing="0">
<tr>
<th> </th>
<th> </td>
<th> </td>
<th>Name</th>
<th>E-mail</th>
</tr>
<% },
(item, css) => { %>
<tr class="<%=css%>">
<td><%=Html.ActionImage("Edit", "Home", new { Id = item.Id }, "~/Content/edit.gif", "Edit")%></td>
<td><%=Html.ActionImage("Delete", "Home", new { Id = item.Id }, "~/Content/delete.gif", "Delete")%></td>
<td> </td>
<td><%=item.Name%></td>
<td><%=item.Email%></td>
</tr>
<% },
"item",
"item-alternating",
item => { %>
<%using (Html.BeginForm("Save", "Home", new { Id = item.Id }, FormMethod.Post, new { id = "editForm" })) {%>
<tr class="item-edit">
<td><input type="image" runat="server" id="save" src="~/Content/ok.gif" alt="Update" /></td>
<td><%=Html.ActionImage("Index", "Home", null, "~/Content/cancel.gif", "Cancel")%></td>
<td> </td>
<td><%=Html.TextBox("Name", item.Name)%></td>
<td><%=Html.TextBox("Email", item.Email)%></td>
</tr>
<% } %>
<% },
data => { %>
<tr>
<td colspan="5"><hr /></td>
</tr>
<tr class="paging">
<td colspan="5">
<% if (data.PagedList.IsPreviousPage) { %>
<%=Html.ActionImage("Show", "Home", new { page = data.PagedList.PageIndex - 1 }, "~/Content/previous.gif", "Previous page")%>
<% } %>
<%=data.PagedList.TotalCount.ToString()%> records
<% if (data.PagedList.IsNextPage) { %>
<%=Html.ActionImage("Show", "Home", new { page = data.PagedList.PageIndex + 1 }, "~/Content/next.gif", "Next page")%>
<% } %>
</td>
</tr>
<tr>
<td colspan="5"> </td>
</tr>
<%using (Html.BeginForm("Add", "Home", FormMethod.Post, new { id = "addForm" })) {%>
<tr>
<th> </th>
<th> </td>
<th> </td>
<th>Name</th>
<th>E-mail</th>
</tr>
<tr>
<td><input type="image" runat="server" id="add" src="~/Content/add.gif" alt="Add" /></td>
<td> </td>
<td> </td>
<td><%=Html.TextBox("Name", "")%></td>
<td><%=Html.TextBox("Email", "")%></td>
</tr>
<% } %>
</tr>
</table>
<% });%>
Razor replaces the brackets <% ... %> with just the # symbol. So for example, this line
<%=Html.TextBox("Email", "")%>
becomes
#Html.TextBox("Email", "")
That's basically it. You'll just have to remove ALL of the <% ... %> brackets.

Determine runtime of Windows Server via ASP.Net

is there a built-in property in any of the ASP.net classes that determines what the uptime of the webserver, where the webapplication is running on, is?
These are some of the possibilities that popped into my mind:
global.asax and the Application_OnStart Event. Get the timestamp and to determine the uptime, I just substract it from DateTime.Now
Write a windows service (automatically startup) which's only purpose is to store the value of the service start somewhere
But neither of these two offer a "built-in" solution. Is there something I might oversee in the .net framework?
To get system uptime, use the following code:
TimeSpan uptime;
using (var uptimeCounter = new PerformanceCounter("System", "System Up Time")) {
uptimeCounter.NextValue();
uptime = TimeSpan.FromSeconds(uptimeCounter.NextValue());
}
EDIT: Note that this can't be used by partially trusted code.
You could use TimeSpan.FromMilliseconds(Environment.TickCount), but it'll wrap after two weeks.
I wrote a server status page in ASP.Net which shows server uptime and more.
Here is the entire page:
<%# Page Title="Server Stats" Language="C#" MasterPageFile="~/Admin.Master" AutoEventWireup="true"
CodeFile="Stats.aspx.cs" Inherits="Stats" %>
<%# Import Namespace="System.Diagnostics" %>
<%# Import Namespace="Microsoft.VisualBasic.Devices" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
<style type="text/css">
body {
background-color: #9DC0E4;
}
table.Details {
width: 550px;
margin-left: -275px;
left: 50%;
position: absolute;
}
table.Details tbody.Group {
border-bottom: solid black 2px;
margin-bottom: 15px;
}
table.Details th.Group {
font-size: x-large;
border-bottom: dashed 1px navy;
}
table.Details th.Name {
text-align: left;
}
table.Details td.Value {
text-align: right;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<%
var computer = new ComputerInfo();
using (var iis = Process.GetCurrentProcess())
using (var cpu = new PerformanceCounter("Processor", "% Processor Time", "_Total"))
using (var uptime = new PerformanceCounter("System", "System Up Time")) {
cpu.NextValue();
uptime.NextValue();
%>
<table class="Details">
<tbody class="Group">
<tr>
<th class="Group" colspan="2">Environment</th>
</tr>
<tr>
<th class="Name">Local server time</th>
<td class="Value">
<%= DateTime.Now.ToString("F")%></td>
</tr>
<tr>
<th class="Name">OS</th>
<td class="Value">
<%= computer.OSFullName%><br />
<%= Environment.OSVersion.ToString()%></td>
</tr>
<tr>
<th class="Name">Machine name</th>
<td class="Value">
<%= Environment.MachineName%></td>
</tr>
<tr>
<th class="Name">User name</th>
<td class="Value">
<%= Environment.UserName%></td>
</tr>
<tr>
<th class="Name">Windows domain</th>
<td class="Value">
<%= Environment.UserDomainName%></td>
</tr>
</tbody>
<tbody class="Group">
<tr>
<th class="Group" colspan="2">IIS</th>
</tr>
<tr>
<th class="Name">IIS Uptime</th>
<td class="Value">
<%= (DateTime.Now- iis.StartTime).ToApproximateString()%></td>
</tr>
<tr>
<th class="Name">Priority</th>
<td class="Value">
<%= iis.PriorityClass%></td>
</tr>
<tr>
<th class="Name">Physical Memory Used</th>
<td class="Value">
<%= ToSizeString(iis.WorkingSet64)%></td>
</tr>
<tr>
<th class="Name">Virtual Memory Used</th>
<td class="Value">
<%= ToSizeString(iis.VirtualMemorySize64)%></td>
</tr>
</tbody>
<tbody class="Group">
<tr>
<th class="Group" colspan="2">Hardware</th>
</tr>
<tr>
<th class="Name">Processors</th>
<td class="Value">
<%= Environment.ProcessorCount.ToString()%></td>
</tr>
<tr>
<th class="Name">Physical memory</th>
<td class="Value">
<%= ToSizeString(computer.TotalPhysicalMemory)%></td>
</tr>
<tr>
<th class="Name">Virtual memory</th>
<td class="Value">
<%= ToSizeString(computer.TotalVirtualMemory)%></td>
</tr>
</tbody>
<tbody class="Group">
<tr>
<th class="Group" colspan="2">Performance</th>
</tr>
<tr>
<th class="Name">Uptime</th>
<td class="Value">
<%= TimeSpan.FromSeconds(uptime.NextValue()).ToApproximateString()%>
</td>
</tr>
<tr>
<th class="Name">CPU Usage</th>
<td class="Value">
<%= (cpu.NextValue()/100).ToString("p")%>
</td>
</tr>
<tr>
<th class="Name">Physical memory free</th>
<td class="Value">
<%= ToSizeString(computer.AvailablePhysicalMemory)%></td>
</tr>
<tr>
<th class="Name">Virtual memory free</th>
<td class="Value">
<%= ToSizeString(computer.AvailableVirtualMemory)%></td>
</tr>
</tbody>
</table>
<%} %>
</asp:Content>
ToSizeString is defined in the .cs file:
protected static string ToSizeString(double bytes) {
var culture = CultureInfo.CurrentUICulture;
const string format = "#,0.0";
if (bytes < 1024)
return bytes.ToString("#,0", culture);
bytes /= 1024;
if (bytes < 1024)
return bytes.ToString(format, culture) + " KB";
bytes /= 1024;
if (bytes < 1024)
return bytes.ToString(format, culture) + " MB";
bytes /= 1024;
if (bytes < 1024)
return bytes.ToString(format, culture) + " GB";
bytes /= 1024;
return bytes.ToString(format, culture) + " TB";
}
ToApproximateString is an extension method defined elsewhere:
public static string ToApproximateString(this TimeSpan time) {
if (time.TotalDays > 14)
return ((int)(time.TotalDays / 7)).ToString("#,0.0") + " weeks";
if (14 - time.TotalDays < .75)
return "two weeks";
if (time.TotalDays > 1)
return time.TotalDays.ToString("#,0.0") + " days";
else if (time.TotalHours > 1)
return time.TotalHours.ToString("#,0.0") + " hours";
else if (time.TotalMinutes > 1)
return time.TotalMinutes.ToString("#,0.0") + " minutes";
else
return time.TotalSeconds.ToString("#,0.0") + " seconds";
}
Thanks, that was the kind of answer I was looking for =)
I'll stick to
TimeSpan _diff = DateTime.Now.Subtract(Process.GetCurrentProcess().StartTime);
this.litRuntime.Text = string.Format("{0} days and {1:00}:{2:00}:{3:00}", (int)_diff.TotalDays, _diff.Hours, _diff.Minutes, _diff.Seconds);
for the moment.

Resources