John
John

Reputation: 3945

If list returns empty display message

Using MVC I am passing a Projects list to the view.

@if (Model.Projects != null && Model.Projects.Count > 0)
{
<fieldset>
    <table class="items" summary="@T("This is a table of the delivery Runs in your application")">
        <colgroup>
}

else
{
//no data available
}

Model.Projects.Count > 0 is saying:

operator > cant be applied to operands of type 'method group' and 'int'

Upvotes: 4

Views: 1137

Answers (4)

tomsullivan1989
tomsullivan1989

Reputation: 2780

You are missing the brackets after Count. Count() is a method not a property, so your code should be:

@if (Model.Projects != null && Model.Projects.Count() > 0)
{
    <fieldset>
        <table class="items" summary="@T("This is a table of the delivery Runs in your application")">
        <colgroup>
}

but seeing as you are only concerned with whether there are any elements in Model.Projects, not how many there are, instead of

Model.Projects.Count() > 0

you could use

Model.Projects.Any()

Upvotes: 1

DGibbs
DGibbs

Reputation: 14618

You are treating Count as if it were a property.

It's a method. You need to call Count(). E.g.

@if (Model.Projects != null && Model.Projects.Count() > 0)
{
<fieldset>
    <table class="items" summary="@T("This is a table of the delivery Runs in your    application")">
        <colgroup>
}

Upvotes: 3

Novice
Novice

Reputation: 2487

Count is a method. Your code must be

Model.Projects.Count()

Upvotes: 3

Stay Foolish
Stay Foolish

Reputation: 3746

how about

Model.Projects.Count() > 0

or

Model.Projects.Any()

if you are using resharper, it will recommend you for Model.Projects.Any()

Upvotes: 5

Related Questions