0

I have assigned some data from database table into my viewbag in controller. Since there is no containing data, my viewbag returns true. Why is this happened?

Controller

//bear in mind that there is no status == 1, all were status == 0
Viewbag.itemlist = db.Furnitures.Where(x => x.Status == 1).ToList(); 

View

@if(Viewbag.itemlist != null)
{
   //The string is displayed even tho it does not contain any data
   <p>I appear</p>
}
LearnProgramming
  • 814
  • 1
  • 12
  • 37
  • 3
    `ToList()` never returns `null`. It returns an empty list. – GSerg Jun 13 '18 at 09:09
  • Because your query cannot return `null` - its returns an empty collection –  Jun 13 '18 at 09:09
  • how do i check whether it is empty collection or not? – LearnProgramming Jun 13 '18 at 09:09
  • How do you check a list is empty? – GSerg Jun 13 '18 at 09:10
  • I usually do `== null` – LearnProgramming Jun 13 '18 at 09:11
  • 1
    Having an empty list is different from not having a list. – GSerg Jun 13 '18 at 09:11
  • If i may ask, what are the syntax to do that? – LearnProgramming Jun 13 '18 at 09:12
  • Possible duplicate of [What does LINQ return when the results are empty](https://stackoverflow.com/q/1191919/11683) and [Check if list is empty in C#](https://stackoverflow.com/q/18867180/11683) – GSerg Jun 13 '18 at 09:13
  • 1
    `@if(Viewbag.itemlist.Any())` or `@if(Viewbag.itemlist.Count > 0)` –  Jun 13 '18 at 09:13
  • 1
    You can just count how many items are in the list to know if it's empty. To make a real-world analogy: How do you know if your shopping bag is empty? You'd count the items in it, of course. And an empty shopping bag is different from not actually having a bag at all (this scenario would be the equivalent of null in the code) – ADyson Jun 13 '18 at 09:17
  • Thank you @StephenMuecke for the straightforward answers. Unfortunately `@if(Viewbag.itemlist.Any())` does not work, have an error says `does not contain a definition for 'Any''` but the other solution (Count>0) works. – LearnProgramming Jun 13 '18 at 09:17

1 Answers1

3

If you want to check if a List is empty, try this:

@if( ((List<Furnitures>) Viewbag.itemlist).Count > 0)
{
    //The string is displayed even tho it does not contain any data
    <p>I appear</p>
}

or

@if( ((List<Furnitures>) Viewbag.itemlist).Any())
{
    //The string is displayed even tho it does not contain any data
    <p>I appear</p>
}

Update:

As pointed out by @learnprogramming, the second solution doesn't work. .Any() doesn't operate on a List, it operates on an IEnumerable.

To make it works you need to add

@using System.Linq

to the top of your view file. Thanks to @ColinM for the tip.

Update 2

Another tip from @Colin. MVC has full support for model binding between Controllers and Views.

It's way better to pass data with model binding instead of ViewBag. In your ActionResult you should do this:

var furnituresList = db.Furnitures.Where(x => x.Status == 1).ToList();
return View(furnituresList);

Then in your view put this on top (after the @using directives):

@model List<Furnitures>

And then check with this:

@if(Model.Count > 0)
{
    //The string is displayed even tho it does not contain any data
    <p>I appear</p>
}
GiampaoloGabba
  • 1,428
  • 1
  • 15
  • 21
  • the second solution does not work though...error saying `does not contain a definition for 'Any''`...the other solution work fine – LearnProgramming Jun 13 '18 at 09:19
  • The second solution actually will work if you add `@using System.Linq` to the top of your view file. `Any()` is an extension method of `IEnumerable` which `List` implements. – ColinM Jun 13 '18 at 09:28
  • Another recommendation would be to provide an option in your answer for Model binding, rather than using `ViewBag` and casting. What if `itemList` isn't a `List` due to developer error at some point ;-) – ColinM Jun 13 '18 at 09:34