Zachary Hanshaw
Zachary Hanshaw

Reputation: 131

If Statement to remove 0 Value Count in C#

I am pulling data from a Warehouse database and displaying on a webpage.

This pulls all data.

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

I need to display each category where the Count is greater than 0.

I tried:

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

Upvotes: 0

Views: 631

Answers (7)

Brendan Vogt
Brendan Vogt

Reputation: 26028

You can try the following. I first checked if there are items in the list and if there are create the HTML table, loop through the items and create a table row for each item:

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

I hope this helps.

Upvotes: 0

Zachary Hanshaw
Zachary Hanshaw

Reputation: 131

Got it, thanks for all the feedback. Here is my final solution.

            @if(categoryModel.Count > 0)
            {
                <td>@Html.ActionLink(categoryModel.Name, "Category","~WarehouseCatalog", new { id = categoryModel.Id }, null)</td>
                <td>@categoryModel.Count</td>
            }

Upvotes: 0

Rion Williams
Rion Williams

Reputation: 76557

You could explicitly filter out the items that you are iterating through using a Where() clause :

<!-- Only iterate through those with Counts greater than 0 -->
@foreach (WarehouseCategoryModel categoryModel in Model.Categories.Where(c => c.Count > 0))
{
     <!-- Do work here -->
}

If you wanted to handle scenarios where your Model or Categories were possibly null, you could add the following explicit check :

<!-- Ensure you have categories to loop through (avoids null exceptions) -->
@if(Model?.Categories != null)
{
      <!-- Only iterate through those with Counts greater than 0 -->
      foreach (WarehouseCategoryModel categoryModel in Model.Categories.Where(c => c.Count > 0))
      {
           <!-- Do work here -->
      }
}
else 
{
    <tr>
        <td>No Categories Available</td>
    </tr>
}

If you were planning on handling this, you would likely want to include this logic within your ViewModel itself as opposed to in the View, but the general idea is the same.

Upvotes: 3

Greg
Greg

Reputation: 11480

You can do this a couple of different approaches, the approach your taking please keep in mind will be slow with complex iterations. I'll point out the potential code errors:

  1. @if will trigger proper Razor, unless you have no Html inbetween your foreach and the if.

Sample:

// Valid
@foreach(var .... model.Categories)
{
     if(...)
     {

     }
}

// Invalid:
@foreach(var .... model.Categories)
{
     <td>Content</td>
     if(...)
     {

     }
}
  1. Your model should be fully qualified, Example.Models.Warehouse.

You should show how you're actually declaring your @model for the View. Which could help us distinguish further, also are you sure that you have more than one? I would structure like this to hold the table row data better:

@if(Model != null)
{
    foreach(Example.Models.Warehouse model in Model.Categories)
    {
       <tr>
           <td>@model.Something</td>
       </tr>
    }
}

There isn't a need for a conditional, the loop won't execute if it is empty.

Upvotes: 0

Jitendra Tiwari
Jitendra Tiwari

Reputation: 1691

Remove @ from IF statement. You have already used in foreach loop.

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

Upvotes: 0

Khalil Khalaf
Khalil Khalaf

Reputation: 9407

As mentioned in the comments. This should work:

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

Upvotes: 0

Brian Herbert
Brian Herbert

Reputation: 1191

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

Upvotes: 1

Related Questions