Zachary Hanshaw Zachary Hanshaw - 7 months ago 11
HTML Question

Writing an If Statement to help remove 0 Value Count in C#

All -

I am pulling data from a Warehouse database and displaying on a webpage. I need to be able to display each category where the Count is greater than 0.

@foreach (WarehouseCategoryModel categoryModel in Model.Categories)
{
<tr>
<td>@Html.ActionLink(categoryModel.Name, "Category", "-WarehouseCatalog", new { ide = categoryModel.ID }, null)</td>
<td>@categoryModel.Count</td>
</tr>
}


This currently works pulling all data. However, I tried:

if(@categoryModel.Count > 0)
{
<td>@categoryModel.Count</td>
}


This didn't work. Any suggestions?

Thanks!

Answer

You have got the @ in the wrong place. It should come before the if, like this

@if(categoryModel.Count > 0)
{
    <td>@categoryModel.Count</td>
}
else
{
    <td></td>
}