I have a foreach displaying product descriptions and product prices. i want to be able to sum all of the prices and display them in a data cell. How can i do this?
<table class="table table-striped">
<tr>
<th>Description</th>
<th>Price</th>
</tr>
#foreach (var product in Model)
{
<tr>
<td>#product.Description</td>
<td>#product.Price</td>
</tr>
}
#foreach (var product in Model)
{
var sum = product.Price++;
}
<tr>
<td colspan="2">Total Price: </td>
</tr>
</table>
i want to be able to show the value of sum outside the foreach
You can declare a variable inside a code block and use it in the foreach like so:
<table class="table table-striped">
<tr>
<th>Description</th>
<th>Price</th>
</tr>
#foreach (var product in Model)
{
<tr>
<td>#product.Description</td>
<td>#product.Price</td>
</tr>
}
#{
var sum = 0;
foreach (var product in Model)
{
sum += product.Price;
}
}
<tr>
<td colspan="2">Total Price: #sum</td>
</tr>
</table>
An alternative could be using System.Linq.
<tr>
<td colspan="2">Total Price: #Model.Sum(product => product.Price)</td>
</tr>
Related
I have a table with a for each loop that displays a number of products:
<table id="myTable" class="table table-bordered">
<thead class="thead-green">
<tr>
<th>Product Name</th>
<th>Product Model</th>
<th>Description</th>
<th>Price</th>
<th>Add Service</th>
</tr>
</thead>
<tbody>
#if (Products.Any())
{
#foreach (var product in Products)
{
<tr>
<td>#product.Name</td>
<td>#product.Model</td>
<td>#product.Description</td>
<td>#product.Price</td>
<td><button style="background-color:#0275d8;" class="btn btn-primary btn-medium" #onclick="#( () => add(product))">Add</button></td>
</tr>
}
}
else
{
<tr><td colspan="6"><strong>No products available</strong></td></tr>
}
</tbody>
</table>
I would like to add a Search bar at the top of it and be able to search the items inside the table
If-else condition is not working in the view page.
In my view page in two table data
tbl_career
tbl_vacancy
currently, tbl_career is empty. So I want to display currently no content available.
currently tbl_vacancy is having 2 records.
Output:
HomeController.cs:
public ActionResult Index(tbl_carrer _Carrer)
{
var model = new CarrerViewModal
{
tbl_Carrers = _dbfltEntities.tbl_carrer.ToList(),
tbl_Qetintouches = _dbfltEntities.tbl_qetintouch.ToList(),
};
//if (_Carrer == null)
//{
// ViewBag.message = "No Content Available";
//}
return View(model);
}
ViewModal:
namespace flatAd.Models
{
public class CarrerViewModal
{
public List<tbl_carrer> tbl_Carrers { get; set; }
public List<tbl_qetintouch> tbl_Qetintouches { get; set; }
}
}
Index.cshtml:
#model flatAd.Models.CarrerViewModal
#{
ViewBag.Title = "Index";
}
<h2>Show Career Data</h2>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>UserCarrerID</th>
<th>UserEmailID</th>
<th>UserContactNo</th>
<th>UserResume</th>
</tr>
</thead>
<tbody>
#foreach (var itemcarrer in Model.tbl_Carrers)
{
if (Model != null)
{
<tr>
<td>#itemcarrer.usercarrerid</td>
<td>
#itemcarrer.useremailid
</td>
<td>
#itemcarrer.usercontactno
</td>
<td>
itemcarrer.userresume
</td>
</tr>
}
else
{
<tr>
<td><label>Content Not Available</label></td>
</tr>
}
}
</tbody>
</table>
<h2>Show GetInTouch Data</h2>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>Name</th>
<th>EmailId</th>
<th>PhoneNo</th>
<th>Description</th>
</tr>
</thead>
#foreach (var itemgetintouch in Model.tbl_Qetintouches)
{
<tr>
<td>#itemgetintouch.name</td>
<td>
#itemgetintouch.emailid
</td>
<td>
#itemgetintouch.phonenumber
</td>
<td>
#itemgetintouch.description
</td>
</tr>
}
</table>
What I am trying:
list is providing the count variable but also not working
#foreach (var itemcarrer in Model.tbl_Carrers)
{
if (Model.tbl_Carrers.Count == 0)
{
<tr>
<td><label>Content Not Available</label></td>
</tr>
}
else
{
<tr>
<td>#itemcarrer.usercarrerid</td>
<td>
#itemcarrer.useremailid
</td>
<td>
#itemcarrer.usercontactno
</td>
<td>
#itemcarrer.userresume
</td>
</tr>
}
}
I am trying the following as well, but it's also not working:
#*#if (Model == null)
{
<tr>
<td><label>Content Not Available</label></td>
</tr>
}*#
#*else
{
foreach (var itemcarrer in Model.tbl_Carrers)
{
<tr>
<td>#itemcarrer.usercarrerid</td>
<td>
#itemcarrer.useremailid
</td>
<td>
#itemcarrer.usercontactno
</td>
<td>
#itemcarrer.userresume
</td>
</tr>
}
}*#
The following condition is also not working:
#foreach (var itemcarrer in Model.tbl_Carrers)
{
if (Model.tbl_Carrers != null)
{
<tr>
<td>#itemcarrer.usercarrerid</td></tr>
}
else
{
<tr>
<td><label>Content Not Available</label></td>
</tr>
}
Is any thing wrong with the if-else condition?
If works. Any code inside a foreach loop won't get executed if there are no data. The if in this loop will never run if tbl_Carrers is empty.
#foreach (var itemcarrer in Model.tbl_Carrers)
{
if (Model.tbl_Carrers.Count == 0)
{
}
...
The check and loop should be reversed:
if (Model.tbl_Carrers.Count == 0)
{
<tr>
<td colspan="4"><label>Content Not Available</label></td>
</tr>
}
else
{
foreach(.....)
{
}
}
Notice the colspan attribute. Without it, the label would appear in the first column only
Based on what you said, you need to find a way to check for tbl_Carrers, but it seems in your code you have check for Model itself, don't forget that Model is CarrerViewModal in your case. With that being said the following line shouldn't work since you've checked for Model itself:
#foreach (var itemcarrer in Model.tbl_Carrers)
{
if (Model != null)
In the following line you've check for tbl_Carrers but the foreach code is not running at all as you don't have enough data in your tbl_Carrers so the if condition is not executed again:
#foreach (var itemcarrer in Model.tbl_Carrers)
{
if (Model.tbl_Carrers.Count == 0)
So what you need is something like this:
if (Model.tbl_Carrers.Count == 0)
{
<tr>
<td><label>Content Not Available</label></td>
</tr>
}
else
{
//iterating through data using the foreach loop
}
I have been searching for quite some time to create the following table through code-behind using C#. I am finding difficulty in adding a <tr> to <thead> and <tr> to <tbody>. This is how the table should look like:
<table id="mytable" border="1" cellpadding="10" cellspacing="0" >
<caption>myCaption</caption>
<thead>
<tr>
<td> </td>
<th scope="col">myHeader1</th>
<th scope="col">myHeader2</th>
<th scope="col">myHeader3</th>
</tr>
</thead>
<tbody>
<tr>
<td>2</td>
<td>3</td>
<td>1</td>
</tr>
</tbody>
</table>
Any help is appreciated.
Create a strongly typed view with a list of whatever you're binding, and use the #foreach loop:
#model List<MyClass>
<table id="mytable" border="1" cellpadding="10" cellspacing="0" >
<caption>myCaption</caption>
<thead>
<tr>
<td> </td>
<th scope="col">myHeader1</th>
<th scope="col">myHeader2</th>
<th scope="col">myHeader3</th>
</tr>
</thead>
<tbody>
#foreach(var m in Model)
{
<tr>
<td>#m.MyProp1</td>
<td>#m.MyProp2</td>
<td>#m.MyProp3</td>
</tr>
}
</tbody>
I have done similar thing using C# MVC. Here is a sample code. Hope this will help to get an idea.
#{int count = Model.HeadingsList.Count;}
<table>
<thead>
<tr>
#foreach (string heading in Model.HeadingsList)
{
<th>#heading</th>
}
</tr>
</thead>
<tbody>
#foreach (string row in Model.rowsList)
{
string[] cellValues = Array.ConvertAll(row.Split(','), p => p.Trim());
if (count != cellValues.Count()) { return; }
<tr>
#for (int i = 0; i < #count; i++)
{
<td>#cellValues[i]</td>
}
</tr>
}
</tbody>
</table>
I had a list of rows like below.
Array[0]['cell1','cell2','cell3'...]
Array[1]['cell1','cell2','cell3'...]
Below is my razor code for my table of users and the current add/edit layout
<table class="smalltable" cellpadding="0" cellspacing="0" id="tblUsers">
<thead>
<tr>
<th> </th>
<th>First Name</th>
<th>Last Name</th>
<th>Dept</th>
<th>Assyst Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>#{ Html.BeginForm("AddEditUser", "Home"); }
<input type="submit" value="Add" id="btnSave" name="cmd" class="plain-button" />
</td>
<td>#Html.Editor("Forename")</td>
<td>#Html.Editor("Surname")</td>
<td>#Html.DropDownList("lstDepts")</td>
<td>#Html.Editor("AssystName")
#{ Html.EndForm(); }</td>
</tr>
#foreach (var User in Model)
{
<tr>
<td>#Html.ActionLink("Edit", "AddEditUser", new { id = User.ID }, new { name = "cmd", value = "Edit" })</td>
<td>
#User.Forename
</td>
<td>
#User.Surname
</td>
<td>
#User.Dept
</td>
<td>
#User.AssystName
</td>
</tr>
}
</tbody>
</table>
What i want to achieve is when a edit is pressed, the row that the edit link was on turns into edit boxes, i how to load up the add form with the edit users details (i could use hidden id and set it with querystring) but i wanted to edit in place really
anyone know how to do this or can point me in the right place?
THanks
<div>
<table >
<tr>
<th>Customer ID</th>
<th>Name</th>
<th>Type</th>
</tr>
#foreach (var a in Model.Attachments)
{
<tr>
<td>
#a.CId
</td>
<td>
#a.CName
</td>
<td>
#a.CType
</td>
</tr>
}
</table>
#Html.PagedListPager((IPagedList)Model.Attachments, page => Url.Action("Index", new { page }))
</div>
Currently I am displaying 25 items per page. If the final page does not have 25 items, I want to append rows to the end of the table, to keep the Page selector at the same level from page to page.
This seems like it would work, I just don't know where to put it:
#for (int i = 25; i > Model.Attachments.Count() % 25; i--)
{
<tr>
<td></td>
</tr>
}
You have for sure a loop thrown the attachments list.
Put this loop right after the loop where you write your TRs.
Just take into account that if your data makes your TRs higher, this will break your solution. Other thing you may try is adding a HTML space in your dummy rows:
<div>
<table >
<tr>
<th>Customer ID</th>
<th>Name</th>
<th>Type</th>
</tr>
#foreach (var a in Model.Attachments)
{
<tr>
<td>
#a.CId
</td>
<td>
#a.CName
</td>
<td>
#a.CType
</td>
</tr>
}
#for (int i = 25; i > Model.Attachments.Count() % 25; i--)
{
<tr>
<td></td>
</tr>
}
</table>
#Html.PagedListPager((IPagedList)Model.Attachments, page => Url.Action("Index", new { page }))
</div>