How to use if statment in View (#doc.value == "N/A" ) - asp.net-mvc

I am trying to use a if statement to display records in a cell where the field DocumentType = "N/A" and then again in a different cell where the field DocumentType <> "N/A". How can this be done. I can not seem to get it correct. Here is what I am trying.
#foreach (Document documents in Model.Documents)
{
if (#documents.DocumentType == "N/A")
{
<tr>
<td class="auto-style1" style="width: 1px">#documents.Description</td>
</tr>
}
}
#foreach (Document documents in Model.Documents)
{
if (#documents.DocumentType != "N/A")
{
<tr>
<td class="auto-style1" style="width: 1px">#documents.Description</td>
</tr>
}
}

To me the approach looks ok. But it sounds like it will be better to use linq.
The list can be filtered on the foreach statement:
#foreach (Document documents in Model.Documents.Where(d => d.DocumentType == "N/A"))
{
<tr>
<td class="auto-style1" style="width: 1px">#documents.Description</td>
</tr>
}
#foreach (Document documents in Model.Documents.Where(d => d.DocumentType != "N/A"))
{
<tr>
<td class="auto-style1" style="width: 1px">#documents.Description</td>
</tr>
}

Related

How to obtain a space between two elements in DataTable?

I am printing the data from ViewModel into DataTable in the following way, but how to obtain a space between #item.FirstName and #item.LastName so that the output can be of the form FirstName LastName? At present, I am getting the Name as FirstNameLastName. Ex: MarkStevens. I want to print it as Mark Stevens.
#if(Model.Count() != 0)
{
<thead>
<tr>
<th data-column-id="Name">Name</th>
...
</tr>
</thead>
}
else
{
<h3 style="text-align:center">No Records Found</h3>
}
<tbody>
#foreach(var item in Model)
{
<tr>
<td>
#(if (item.UserId != null)
{
#item.FirstName #item.LastName
}
else
{
#item.OtherUser
}
</td>
</tr>
}
</tbody>
You can use #Html.Raw helper which means, you render whatever inside without any html encoding like space or string:
#item.FirstName #Html.Raw(" ") #item.LastName
If you want html, you can also use a simple non-breaking spaces:
#item.FirstName #item.LastName
How about string format the output: $"{#item.FirstName} {#item.LastName}"
use string.Format. i feel this is very clean approach.
string.Format("{0} {1}", #item.FirstName,#item.LastName)

how to add data in html table in .net mvc view from controller

i just want to add two columns dynamically to my table. using mvc razor.. and this is my code below :-
<table>
<tr>
<th>id</th>
<th>name</th>
<th>type</th>
<th>delete</th>
<th>modified</th>
and for getting data to my table m using code like :-
<tbody>
#foreach (System.Data.DataRow row in Model.Rows )
{
<tr>
#foreach (var in cell in row.ItemArray)
{
if (cell ! = null)
{
<td>#cell.ToString()</td>
}
else {
<td></td>
}
}
</tr>
}
<tbody>
</table>
now i just want to add one link in both delete and modified columns for each rows.. thanks for any help
You can add buttons in columns after cells foreach cycle. I also sugest you to use a PartialView for button display but this depends on you. Below is the simple way of doing it. I also used some bootstrap classes.
<tbody>
#foreach (System.Data.DataRow row in Model.Rows )
{
<tr>
#foreach (var in cell in row.ItemArray)
{
if (cell ! = null)
{
<td>#cell.ToString()</td>
}
else {
<td></td>
}
}
<td>
<a class="btn btn-primary btn-sm" href="#Url.Action("Edit", "ABC", new { id=Model.Id})" title="Edit ABC"><i class="glyphicon glyphicon-edit"></i></a></td>
<td>
<a class="btn btn-info btn-sm" href="#Url.Action("Details", "ABC", new { id=Model.Id })" title="Details of ABC"><i class="glyphicon glyphicon-file"></i></a></td>
<td>
<a class="btn btn-danger btn-sm" href="#Url.Action("Delete", "ABC", new { id=Model.Id })" title="Delete this ABC"><i class="glyphicon glyphicon-remove"></i></a></td>
</tr>
}
</tbody>
Just add more cells after the inner loop:
<tr>
#foreach (var in cell in row.ItemArray) {
if (cell ! = null) {
<td>#cell.ToString()</td>
} else {
<td></td>
}
}
<td>#* Render something at the end of the row*#</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()

Create N*M <table> in view by iterating through model items

Well, easily I have a model:
#model IEnumerable<CompanyWebSite.Category>
And it's quite straightforward to iterating through its items and create a One-item-per-row table to show them.
And the problem is that I want to create a table like this:
<table>
<tr>
<td></td>
<td></td>
</tr>
//
// ...
//
<tr>
<td colspan="2"> ... LastItem ...</td>
</tr>
</table>
Seems simple, but I can't do that...!
#{
ViewBag.Title = "Products";
var last = Model.Last();
int i = 1;
}
<table id="tblCats">
#foreach(var item in Model)
{
if (i == 1){
<tr>
#(i = 2)
}
#if (!item.Equals(last))
{
<td>
<h4>#item.CategoryName</h4>
<label>#item.Description</label>
</td>
if (i == 2)
{
</tr>
i = 1;
}
else
i = 2;
}
else
{
<td colspan="2">
</td>
</tr>
}
}
Because of the first opening <tr>, Razor gets confused and doesn't see the closing }s and else statements... What can I do...?!
You could use HtmlHelper.Raw method to help razor

Insert "where" rule in a "foreach" search

I need some help in order to put a where rule into the foreach search. My goal is to exclude orders where the customerOrder.ERPOrderNumber starts with letter E
The code that i have returns all the orders for the specific customer.
Thank you in advance for your help.
#foreach (var customerOrder in Model.CustomerOrders)
{
<tr>
<td class="mavo-order-date">#customerOrder.OrderDate.ToShortDateString()
</td>
<td class="mavo-status">#customerOrder.Status
</td>
<td class="mavo-order-number">
#customerOrder.OrderNumber
</td>
#if (Model.ShowErpOrderNumber)
{
<td class="mavo-erp-order">#customerOrder.ERPOrderNumber
</td>
}
<td class="mavo-po">#customerOrder.CustomerPO
</td>
<td class="mavo-order-total">#customerOrder.OrderGrandTotal.ToCurrency()
</td>
<td class="mavo-view">
<a class="btn btnStyleA tbm5" href="#Url.Action("OrderHistoryDetail", "Account", new { orderId = customerOrder.ERPOrderNumber })">View Details</a>
</td>
</tr>
}
You can put an if statement inside the foreach loop to only write out the table row when the order number doesn't start with the letter E.
#foreach (var customerOrder in Model.CustomerOrders)
{
#if(!customerOrder.ERPOrderNumber.StartsWith("E"))
{
// Markup goes in here
}
}
Or you can use LINQ to filter the CustomerOrders collection.
#foreach(var customerOrder in Model.CustomerOrders.Where(x => !x.StartsWith("E"))

Resources