How can I arrange items in a table - MVC3 view (Index.cshtml) - c#

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 ...

Related

Summing by multiple columns in a list

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>
}

asp.net mvc viewdata display in a viewpage

I sent a viewdata to a view page like this.
public ActionResult SelectRes(int id)
{
var ResList = resrepository.GetAll();
ViewData["ResList"] = new SelectList(ResList, "ResId", "Res_KORNM");
return View(svcresrelationRepository.GetAll());
}
#foreach (var item in ViewData["ResList"] as List<ITSDapper.Dapper.Resource>)
{
<tr>
<td style="text-align:center;">
#Html.DisplayFor(a=> item.Res_KORNM)
</td>
</tr>
}
And I tried to display the viewdata in a view page but it's not worked.
(Object reference not set to an instance of an object)
How display the viewdata in foreach statement?
You would typically use a SelectList for data that is to be selected by a user.
If this is the intention you can just use an Html.DropDownList:
#Html.DropDownList("SelectedId",
(IEnumerable<SelectListItem>)ViewData["ResList"])
If you are simply needing to view the data, I would change your server-side code to use something other than a SelectList.
This could be done as follows:
Controller
public ActionResult SelectRes(int id)
{
var ResList = resrepository.GetAll();
ViewData["ResList"] = ResList;
return View(svcresrelationRepository.GetAll());
}
View
#foreach (var item in ViewData["ResList"] as List<ITSDapper.Dapper.Resource>)
{
<tr>
<td style="text-align:center;">
#Html.DisplayFor(a=> item.Res_KORNM)
</td>
</tr>
}
You are converting reference to wrong type, you are creating SelectList instance while in View you are convering it to IList<T>, you should convert it to SelectList:
#foreach (var item in ViewData["ResList"] as SelectList)
{
<tr>
<td style="text-align:center;">
#Html.DisplayFor(a=> item.Text) // note this as well
</td>
</tr>
}

asp.net mvc foreach many to many filter

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;
}
}

linq to entity query returns list as continuous string

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 />");

Is it possible to utilize the LINQ portion of the foreach inside the loop?

I have this view in an ASP.NET MVC application:
<%
var path = Path.Combine(HttpRuntime.AppDomainAppPath, "uploads");
foreach (var file in Directory.GetFiles(path).OrderBy(f => new FileInfo(f).Length))
{
var item = new FileInfo(file);
%>
<tr>
<td></td>
<td>
<%=Html.Encode(Path.GetFileName(item.Name))%>
</td>
<td>
<%=Html.Encode(Functions.FormatBytes(item.Length))%>
</td>
<td>
<%=Html.FileLink(item.Name)%>
</td>
</tr>
<% } %>
Is it possible to access my variable f inside the loop, or is there some other way to do this so I do not have to dimension two instances of FileInfo(file)?
Thanks!
var fileInfos = new DirectoryInfo(path).GetFiles().OrderBy(f => f.Length);
foreach (var fileInfo in fileInfos)
{
...
}
Your view is really overstepping it's responsibilities here. You should create a class that maps to what data you want to show in the view, then in your controller, retrieve the data and populate a IEnumerable<> of your classes and return that in the ViewModel, which your view can then just simple loop through.

Categories