Reputation: 89
If-else condition is not working in the view page.
In my view page in two table data
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:
@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?
Upvotes: 0
Views: 83
Reputation: 39956
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
}
Upvotes: 1
Reputation: 131364
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
Upvotes: 2