I'm trying to loop thru if else condition where if they are current show the item and if not show the message that there are no items. The problem i am having is the message keeps showing no matter what. What am I doing incorrectly?
I've tried put the message outside of the loop completely and tried else if (iscurrent=false) and in both instances the message still shows.
<div class="col-md-6">
<ul>
@foreach (var item in Model.Items)
{
if (item.IsCurrent == true)
{
<li>
@item.Id
</li>
}
else if (item.IsCurrent == false)
{
@: There is not a current Item at this time. Do you want
to
<a asp-area="Admin" asp-controller="Item" asp-
action="CreateItem"> add a Item?</a>
}
}
</ul>
</div>
I expect to only show the items that are set to IsCurrent only and when there are no items the message to show only.
I believe this would be closer to what you want:
<div class="col-md-6">
<ul>
@if (Model.Items.Any(x => x.IsCurrent))
{
foreach (var item in Model.Items)
{
if (item.IsCurrent)
{
<li>
@item.Id
</li>
}
}
}
else
{
@:There is not a current Item at this time. Do you want to
<a asp-area="Admin" asp-controller="Item" asp-action="CreateItem">add a Item?</a>
}
</ul>
</div>
First it checks for any items that are current using .Any()
, then loops through the Model.Items
and displays the current ids. If not, it displays the no current item message.