[英]If-else condition does not work as per expectation
If-else condition is not working in the view page. If-else 条件在视图页面中不起作用。
In my view page in two table data在我的视图页面中有两个表数据
currently, tbl_career is empty.目前,tbl_career 为空。 So I want to display currently no content available .
所以我想显示当前没有可用的内容。 currently tbl_vacancy is having 2 records.
目前 tbl_vacancy 有 2 条记录。
Output:输出:
HomeController.cs: 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:索引.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? if-else条件有什么问题吗?
If
works. If
有效。 Any code inside a foreach
loop won't get executed if there are no data.一个内部的任何代码
foreach
如果没有数据环路不会得到执行。 The if
in this loop will never run if tbl_Carrers
is empty.如果
tbl_Carrers
为空, if
此循环中的if
将永远不会运行。
@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.注意
colspan
属性。 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.根据您所说的,您需要找到一种方法来检查
tbl_Carrers
,但是在您的代码中似乎您已经检查了Model
本身,不要忘记Model
在您的情况下是CarrerViewModal
。 With that being said the following line shouldn't work since you've checked for Model
itself:话虽如此,由于您已经检查了
Model
本身,因此以下行不应该起作用:
@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:在以下行中,您已检查
tbl_Carrers
但 foreach 代码根本没有运行,因为您的tbl_Carrers
没有足够的数据,因此不会再次执行if
条件:
@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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.