I am creating a html table to display values in a List<> the list has three columns: the name of the indicator, the year and month in which it was selected and the count of the number of times it has been filtered on.
the code and html I am using is shown below:
<table class="users">
<tr>
<th></th>
#foreach (var monthYear in Model.KeyIndicatorUseCount.Select(m => m.MonthYear).Distinct())
{
<th>#monthYear</th>
}
</tr>
#foreach (var keyIndicator in Model.KeyIndicatorUseCount.Select(m => m.Name).Distinct())
{
<tr>
<td>#keyIndicator</td>
#foreach (var monthYear in Model.KeyIndicatorUseCount.Select(m => m.MonthYear).Distinct())
{
<td>#Model.KeyIndicatorUseCount.Sum(m => m.Count)</td>
}
</tr>
}
</table>
Currently the table is being populated with the sum of the values for the first month-year column.
The calculation needs to be the sum of the number of views for each key indicator with that month-year.
For reference here is a screenshot of what is currently being produced:
How do I go about performing the calculation to get a sum which corresponds to each month-year for the key indicator name in the first column?
#foreach (var keyIndicator in item.OrderBy(f => f.Name).GroupBy(f => f.Name))
{
<tr>
<td>#keyIndicator.Key</td>
#foreach (var monthYear in keyIndicator.OrderBy(f => f.MonthYear).GroupBy(f => f.MonthYear))
{
<td>#(keyIndicator.Sum(f => f.Count))</td>
}
<td>
#(keyIndicator.Sum(f => f.Count)) /* if you want to sum count of every month of year */
</td>
</tr>
}
Related
Hi this probably is an easy question but i cant find information about how to solve it i have a table with a field named Email and the values are of type string but the problem is that mvc or the browser automatically changes that string email into Hyperlink as shown in the following picture
when i inspect the element it is an hyperlink:
lacubana#la.com
what can i do to only display the emails as string? i don't want that information to be in hyperlink format. thanks very much
Edited: here is my code of the view
<table class="table table-bordered table-striped">
<tr>
<th>Email</th>
<th>Contraseña</th>
<th>NickName</th>
<th>TipoUsuario</th>
<th>Acciones</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>#Html.DisplayFor(modelItem => item.Email)</td>
<td>#Html.DisplayFor(modelItem => item.Contraseña)</td>
<td>#Html.DisplayFor(modelItem => item.NickName)</td>
#if (item.TipoUsuario == 1)
{
<td>Administrador</td>
}
else
{
<td>Vendedor</td>
}
<td>
#Html.ActionLink("Editar", "EditarUsuario", new { id = item.IdUser }) |
#Html.ActionLink("Eliminar", "EliminarUsuario", new { id = item.IdUser })
</td>
</tr>
}
</table>
and here is the code of my controller:
IList<Usuario> UsuarioList = new List<Usuario>();
var query = from usu in database.ReportingUsersT
where usu.Activo == 1
select usu;
var listdata = query.ToList();
foreach (var Usuariodata in listdata)
{
UsuarioList.Add(new Usuario()
{
IdUser = Usuariodata.IdUser,
Email = Usuariodata.Email,
Contraseña = Usuariodata.Contraseña,
NickName = Usuariodata.NickName,
TipoUsuario = Usuariodata.TipoUsuario
});
}
return View(UsuarioList);
#Html.DisplayFor(...) is determining that the text is an email and is wrapping it in a link. You can simply use
<td>#item.Email</td>
to display it as text
I currently have a for-loop in my .cshtml file which iterates through items inside a ViewBag. I want to sort these objects based on a DateTime property called orderDate in the Order.cs file. The for loop in the .cshtml file looks like the following:
<table id="order-history-table" class="table table-hover">
<thead>
<tr>
<th>
Order Number
</th>
<th>
Order Date
</th>
<th>
Order Total
</th>
</tr>
</thead>
<tbody>
#foreach (var item in ViewBag.list)
{
<tr>
<td>
<a class="orderNumber" data-toggle="modal" data-target="#modal-container" href="#Url.Action("orderDetails", "Orders", new { orderNumber = item.order.OrderNumber })">#item.order.OrderNumber</a>
<br />
#Html.ActionLink("Re-Order", "ReOrder", new { orderNumber = #item.order.OrderNumber }, new { onclick = "return confirm('Are you sure you wish to re-order?');" })
</td>
<td>#item.order.OrderDate.ToString("dd/MM/yyyy")</td>
<td style="text-align: center">$#(Math.Round(item.order.OrderTotal, 2))</td>
</tr>
}
</tbody>
I want this table to be sorted so that the most recent orders are shown at the top of the table. Since the data is being pulled from a database, and the tables values are generated dynamically, where do I perform this sorting? is it done through jQuery? How do I do this?
Thank you in advance
You can use LINQ OrderBy to do that.
So in your action method where you set the ViewBag.list, You can use OrderByDescending method on the collection.
var yourOrderList=new List<Order>();
// to do : Load Order items to yourOrderList
yourOrderList=yourOrderList.OrderByDescending(f=>f.OrderDate).ToList();
ViewBAg.list=yourOrderList;
I also recommend using a view model to pass data from your action method to view.
var yourOrderList=new List<Order>();
// to do : Add orders to yourOrderList
yourOrderList=yourOrderList.OrderByDescending(f=>f.OrderDate).ToList();
return View(yourOrderList);
And in your view
#model List<Order>
<h3>Orders</h3>
#foreach(var o in Model)
{
<p>#o.OrderDate.ToString()</p>
}
Since your view is strongly typed, you can do the same ordering in your razor view also as needed.
#model List<Order>
<h3>Orders</h3>
#foreach(var o in Model.OrderByDescending(g=>g.OrderDate))
{
<p>#o.OrderDate.ToString()</p>
}
My code in view:
#foreach (var item in Model._games)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
<td>
#foreach (var item2 in Model._days.Where(x => x.Games.Any(u=> u.Id == item.Id)))
{
#item2.CutOffTime;
}
</td>
<td>
#Html.DisplayFor(modelItem => item.Price)
</td>
<td>
</td>
</tr>
}
What is the proper way to show second foreach and filter it like I do? If I do not filter(using only Model._days, It shows all Days, but when I use Where filter with ID as you can see in second foreach, it doesn't show anything.
Thanks
Possible issues:
1. You're using deferred loading and forgot to populate .Games property of Model._days by using of Include method (EF) or LoadWith (LINQ 2 SQL)
2. Games is empty
3. Games is not empty but you've set an incorrect filter
4. You've populated either Model._games or Model._days.Games with incorrect data
Troubleshooting: set a breakpoint at the second foreach and see what actual values do you have in both collections.
Change your second foreach to
#foreach (var item2 in Model._days)
{
if(item2.Games.select(e=>e.Id).Cotains(item.Id)
{
#item2.CutOffTime;
}
}
I have a query that looks like this:
var ChangesOthersResult = surveyResponseRepository.Query.Select(r => r.ChangesOthers);
That returns all of the entries in "ChangesOthers" column from my table.
When I return the data inside my view:
#Html.DisplayFor(modelItem => modelItem.ChangesOthersResult)
It is returning all of the data as a single continous string of text. How can I add a line break in between the columns of data that it is returning?
var data = new ResultsViewModel()
{
PatientFollowUpResult = PatientFollowUpResult,
PatientFollowUpResultPct = PatientFollowUpResultPct,
TotalResponsesResult = TotalResponsesResult,
ChangesOthersResult = ChangesOthersResult,
};
View Model Type
#model CMESurvey.ViewModels.ResultsViewModel
Perhaps something like:
#foreach (var x in Model.ChangesOthersResult)
{
#x<br>
}
<table>
<tr>
<th>
HeaderName1
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => modeItem.FieldName)
</td>
</tr>
}
</table>
Razor doesn't know that you want line breaks in between each item when you use
#Html.DisplayFor(modelItem => modelItem.ChangesOthersResult)
You can literally add the line break in the select by adding the string "<br/>" to each item before selecting it:
var ChangesOthersResult = surveyResponseRepository
.Query
.Select(r => r.ChangesOthers + "<br />");
I want to show the amount of different types of vitamins present in specific types of food samples, using ASP.NET MVC3. How can I display this in my View (Index.cshtml) ?
an example:
And these are my codes:
<table>
<tr>
<th></th>
#foreach (var m in Model)
{
foreach (var v in m.Vitamins)
{
<th>#v.Name</th>
}
}
</tr>
#foreach (var m in Model)
{
foreach (var f in m.Foods)
{
<tr>
<td>#f.Type</td>
</tr>
}
}
</table>
#*The amount of Vitamins in each food:
var a in m.AmountOfVitamins
#a.Amount
*#
If the Amount collection is ordered correctly (I'm guessing yes), then you could use simply:
foreach (var f in m.Foods)
{
<tr>
<td>#f.Type</td>
foreach (var a in m.AmountOfVitamins)
{
<td>a.Amount</td>
}
</tr>
}
If they're in some other random order, then you'd need a way to sort them according to the header columns A, B, C, D ...