dynamically generated table did not display - asp.net-mvc

<table>
#if (Model.Logs != null && Model.Logs.Count > 0)
{
<tr>
<th>Operation Name</th>
<th>User</th>
<th>Parameters</th>
<th>Comment</th>
<th>Operation Time</th>
</tr>
foreach (var log in Model.Logs)
{
<tr>
<td>#Html.DisplayFor(model => log.OperationName)</td>
<td>#Html.DisplayFor(model => log.User)</td>
<td>#Html.DisplayFor(model => log.Parameters)</td>
<td>#Html.DisplayFor(model => log.Comment)</td>
<td>#Html.DisplayFor(model => log.OperationTime)</td>
</tr>
}
}
</table>
I'm trying to query some data from db and show them in a table via MVC. I set breakpoints and the codes seem to work well. But at last, the table didn't show on the page as expected. What's the possible reason? Thanks.

Are you sure it is looping when you put break point? If it does, try this. Make sure looping.
<table>
#if (Model.Logs != null && Model.Logs.Count > 0)
{
<tr>
<th>Operation Name</th>
<th>User</th>
<th>Parameters</th>
<th>Comment</th>
<th>Operation Time</th>
</tr>
foreach (var log in Model.Logs)
{
<tr>
<td>#log.OperationName</td>
<td>#log.User</td>
<td>#log.Parameters</td>
<td>#log.Comment</td>
<td>#log.OperationTime</td>
</tr>
}
}
</table>

Change the if-statement to a working statement where it returns something. I probably returns nothing at this moment.

Related

Error "does not contain a definition for 'Select' and no extension method 'Select'

I'm getting one error while building the asp.net mvc solution, but I am able to get the datatable with data, but don't know why im getting this error. Maybe I need to add the LINQ reference somewhere.
Getting near Model.Select.
Code:
#model List<smartpond.Models.FeederPillar>
<table id="listing"
class="table table-bordered table-striped table-responsive table-hover">
<thead>
<tr>
<th>Sl no</th>
<th>Time</th>
<th>R Voltage</th>
<th>Y Voltage</th>
<th>B Voltage</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.Select((value, i) => new { i, value }))
{
<tr class="tr_#item.value.deviceid">
<td>#(item.i + 1)</td>
<td>#item.value.datetime</td>
<td>#item.value.RVoltage</td>
<td>#item.value.YVoltage</td>
<td>#item.value.BVoltage</td>
</tr>
}
</tbody>
</table>
Add the following using statement at the top of your razor page
#using System.Linq
Try this
#foreach (var item in Model)
{
<tr>
<td>#item.id</td>
<td>#item.datetime</td>
<td>#item.RVoltage</td>
<td>#item.YVoltage</td>
<td>#item.BVoltage</td>
</tr>
}

Add count/sub-count to mvc view

I've got a view that is showing the breakdown of all fault categories reported and the area that they've been reported in. What I need to get is a total count of the number of faults of each category, and then a sub count of each area under that type. I've seen around that there are options to use Model.Count(), however whenever I try that it doesn't return the right number.
The view code that I'm using is below:
<table class="table table-responsive">
<tr>
#foreach (var catGroup in Model.GroupBy(item => item.job_category))
{
foreach (var itemCat in catGroup.Take(1))
{
if (itemCat.job_category != null)
{
<td>
<table id="Tbl_Total_#itemCat.job_category">
<tr>
<th>
#Html.DisplayFor(modelItem => itemCat.job_category)
</th>
</tr>
#foreach (var item in catGroup.GroupBy(item => item.job_area))
{
foreach (var itemArea in item.Take(1))
{
<tr>
<td>
#Html.DisplayFor(modelItem => itemArea.job_area)
</td>
</tr>
}
}
</table>
</td>
}
}
}
</tr>
</table>
Can someone point me in the right direction to get a count per category, and per area under the categories?
Thanks
You can get the number of element per group calling Count extension method. eg, for the category groups you can do this:
catGroup.Count()
And you can do the same for the areas:
item.Count()

How to Hide Table in MVC View when there is No Data?

I am populating my datatable using Linq.
I have hard-coded headers. And populating body columns with Linq. Following is my code.
<table id="tableID">
<thead>
<tr>
<th>Name</th>
<th>ID</th>
<th style="width:2%;"></th>
</tr>
</thead>
<tbody>
#if(Model.Values !=null)
{
foreach(var value in Model.Values)
{
<tr>
<td>#value.Name</td>
<td>#value.ID</td>
</tr>
}
}
</tbody>
</table>
What I am thinking to do here, if there is no data table should not be visible.I thought of moving my conditional check if model is returning null prior to table creation but it will throw exception. I am fairly new to MVC. Any suggestion is appreciated.
Thank you
Simply put one if around table as well to check if property is not null and count of that list is greater than 0 then table should be rendered.
#if(Model != null)
{
if(Model.Values != null && Model.Values.Count != 0)
{
<table id="tableID">
<thead>
<tr>
<th>Name</th>
<th>ID</th>
<th style="width:2%;"></th>
</tr>
</thead>
<tbody>
#foreach(var value in Model.Values)
{
<tr>
<td>#value.Name</td>
<td>#value.ID</td>
</tr>
}
</tbody>
</table>
}
}

MVC DisplayFor template table with duplicated headers

Using a EditorFor template and trying to create a table, how can I get only one header row instead of one for each item of the collection?
Being the viewmodel
public class CashbackOfferViewModel
{
public List<SingleCashbackOfferViewModel> Offers { get; set; }
}
In the view I have
#Html.DisplayFor(m => m.Offers)
And the display template is
#using LMS.MVC.Infrastructure
#model LMS.MVC.Areas.Finance.Models.SingleCashbackOfferViewModel
<table class="dataGrid">
<thead>
<tr class="headerRow">
<th>#Html.LabelFor(m => m.CampaignName)</th>
<th>#Html.LabelFor(m => m.PersonId)</th>
<th>#Html.LabelFor(m => m.FullName)</ths>
</tr>
</thead>
<tbody>
<tr>
<td>#Html.DisplayFor(m => m.CampaignName)</td>
<td>#Html.DisplayFor(m => m.PersonId)</td>
<td>#Html.DisplayFor(m => m.FullName)</td>
</tr>
</tbody>
</table>
One solution can be:
Create display template for your whole model
#using LMS.MVC.Infrastructure
#model LMS.MVC.Areas.Finance.Models.CashbackOfferViewModel
<table class="dataGrid">
<thead>
<tr class="headerRow">
<th>CampaignName</th>
<th>PersonId</th>
<th>FullName</ths>
</tr>
</thead>
<tbody>
#for(int i = 0; i < Model.Offers.Count; ++i)
{
<tr>
<td>#Html.DisplayFor(m => m.Offers[i].CampaignName)</td>
<td>#Html.DisplayFor(m => m.Offers[i].PersonId)</td>
<td>#Html.DisplayFor(m => m.Offers[i].FullName)</td>
</tr>
}
</tbody>
</table>
Also i think in your code, your model must be List of SingleCashbackOfferViewModel and you can easily iterate over the list and create table

Getting Incorrect razor syntax errors

I have the following code and I want to put the table headers outside of the #foreach but when i move it outside It no longer sees the closing tag. Thanks
#foreach (var item in Model)
{
<div class="data" ><label for="fullName" >Name: </label>#item.UserName.Replace('.', '')</div>
if (Model.Count > 0)
{
<table class="data">
<tr>
<th>
Work Date
</th>
<th>
Change Details
</th>
<th>
Ip Address
</th>
<th></th>
</tr>
<tr>
<td>
#{
var stringDate = item.WorkDate.ToShortDateString();
}
#Html.DisplayFor(modelItem => stringDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.ChangeDetail)
</td>
<td>
#Html.DisplayFor(modelItem => item.IpAddress)
</td>
</tr>
</table>
}
<br />
<hr />
}
So this is kinda what I'm trying to accomplish, but it keeps giving me errors no matter what way I try it.
<table class="data">
<tr>
<th>
Work Date
</th>
<th>
Change Details
</th>
<th>
Ip Address
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<div class="data" ><label for="fullName" >Name: </label>#item.UserName.Replace('.','')</div>
if (Model.Count > 0)
{
<tr>
<td>
#{
var stringDate = item.WorkDate.ToShortDateString();
}
#Html.DisplayFor(modelItem => stringDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.ChangeDetail)
</td>
<td>
#Html.DisplayFor(modelItem => item.IpAddress)
</td>
</tr>
</table>
}
<br />
<hr />
}
You must have your table closing tag outside of the for each loop.
Short answer: the closing tag </table> has to be placed outside of the foreach loop.
Long answer: You should put the Model.Count > 0 outside of the foreach loop because the moment you enter the code block within the loop, Model.Count > 0 always evaluates to true because you have at least one item.
If – and only if – your model contains any items, you print out the table header. After that, you append one row for each item in Model. Both <table> and </table> have to occur within the same level of nesting.
#if (Model.Count > 0)
{
<table class="data">
<tr>
<th>Date</th>
<th>Change Details</th>
<th>Ip Address</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<div class="data" >
<label for="fullName" >Name: </label>
#item.UserName.Replace('.', ' ')
</div>
#{
var stringDate = item.WorkDate.ToShortDateString();
}
<tr>
<td>#Html.DisplayFor(modelItem => stringDate)</td>
<td>#Html.DisplayFor(modelItem => item.ChangeDetail)</td>
<td>#Html.DisplayFor(modelItem => item.IpAddress)</td>
</tr>
}
</table>
<br />
<hr />
}
You should consider moving the logic (#item.UserName.Replace('.', ' ')) out of the view into the corresponding controller action.

Resources