If-else condition does not work as per expectation - c#

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
}

Related

How can I add a new row in a table every 10 rows, automatically?

I have a table getting data from a model with a foreach, in a razor view.. I want to add a new row every 10 rows, how can I do that?
EDIT
<tbody>
#foreach (var context in sortedData)
{
#for (int i = 1; i % 10 == 0;i++)
{
<tr>
<td>
new row
</td>
</tr>
}
<tr>
<td>
#context.Id
</td>
<td>#context.CantidadElegida</td>
<td>#context.Item</td>
<td>#String.Format("RD${0:f2}", #context.Price)</td>
<td>#String.Format("RD${0:f2}", #context.Reposition)</td>
<td>#String.Format("RD${0:f2}", #context.SubTotal)</td>
</tr>
}
</tbody>
Seems you are looking for this.
int count=1;
#foreach (var context in sortedData)
{
if ( count % 10 == 0)
{
<tr>
<td>
new row
</td>
</tr>
}
<tr>
<td>
#context.Id
</td>
<td>#context.CantidadElegida</td>
<td>#context.Item</td>
<td>#String.Format("RD${0:f2}", #context.Price)</td>
<td>#String.Format("RD${0:f2}", #context.Reposition)</td>
<td>#String.Format("RD${0:f2}", #context.SubTotal)</td>
</tr>
count++;
}
What about this:
int count=1;
#foreach (var context in sortedData)
{
if ( count++ % 10 == 0)
{
<tr>
<td>
new row
</td>
</tr>
}
<tr>
<td>
#context.Id
</td>
<td>#context.CantidadElegida</td>
<td>#context.Item</td>
<td>#String.Format("RD${0:f2}", #context.Price)</td>
<td>#String.Format("RD${0:f2}", #context.Reposition)</td>
<td>#String.Format("RD${0:f2}", #context.SubTotal)</td>
</tr>
}

How to use a razor declared variable outside of razor?

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>

MVC Printing out each Enum value through #foreach

Enum:
public enum Enumenter
{
Products = 10,
Users = 20,
Actions = 30
}
my view:
#model IEnumerable<ServerDB.Tables.EntityType>
#{
ViewBag.Title = "Infrastruktur";
}
<table class="table">
<tr>
<th>
<h2>Infrastruktur</h2>
</th>
<th></th>
</tr>
#foreach (var val in Enum.GetNames(typeof(ServerDB.Tables.Enum)))
{
<tr>
<td>
#Html.DisplayName((String.Format("{0}{1}", val, "2")))
</td>
<td></td>
<td>
<p>
</p>
</td>
<td></td>
</tr>
}
#foreach (var Ost in Enum.(typeof(ServerDB.Tables.Enum)))
{
#Html.DisplayName((String.Format("{0}", Ost)))
}
</table>
How do i Print out The values next to the products?
im going to use the numbers from the enum to make a query search, so i can search for any product, any users or actions for that matter.
im no expert in this, so please be gentle.
Enum.GetValues returns values for the enum as objects, so you need to cast them to int value (using int instead of var will do that for you). After that you can use Enum.GetName to get the name for specific value:
#foreach (int val in Enum.Values(typeof(ServerDB.Tables.Enum)))
{
var name = Enum.GetName(typeof(ServerDB.Tables.Enum), val);
<tr>
<td>
#Html.DisplayName((String.Format("{0}{1}", name, "2")))
</td>
<td>
#val
</td>
<td>
<p>
</p>
</td>
<td>
</td>
</tr>
}
There is another approach which doesn't use the Enum.GetName method. You simply cast the value you get from Enum.Values to string to get the name and to int to get the value
#foreach (var name in Enum.Values(typeof(ServerDB.Tables.Enum)))
{
var val = (int)name;
<tr>
<td>
#Html.DisplayName((String.Format("{0}{1}", name, "2")))
</td>
<td>
#val
</td>
<td>
<p>
</p>
</td>
<td>
</td>
</tr>
}

Adding rows to the final page of a pagedlist

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

Sorting a generated table in my view (visual studio .net)

<h2>Documenten</h2>
<table border="1"">
<tr>
<th">
title
</th>
<th>
description
</th>
<th>
Download
</th>
</tr>*
#foreach (var item in Model.Trajects.Components)
{
if (item.getType() == "document")
{
<tr>
<td>
#item.Title
</td>
<td>
#item.Description
</td>
<td><a href='#Url.Action("Download","Component", new {componentid=item.componentID,l=Request["trajectcode"],g = Model})'>
<img src='#Url.Content("../../Images/play.png")' alt="Home Page">
</a> </td>
</tr>
}
}
</table>
Now, whats the best way to sort this table on title, when I initialize the view?
I prefer to sort it in the controller, but you can do the following:
#foreach (var item in Model.Trajects.Components.OrderBy(c => c.Title))
{
if (item.getType() == "document")
{
<tr>
<td>#item.Title</td>
<td>#item.Description</td>
<td>
<a href='#Url.Action("Download","Component", new { componentid = item.componentID, l = Request["trajectcode"], g = Model })'>
<img src='#Url.Content("../../Images/play.png")' alt="Home Page">
</a>
</td>
</tr>
}
}
See OrderBy in the foreach statment.

Categories

Resources