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>
}
11
  • 3
    ToList() never returns null. It returns an empty list. Commented Jun 13, 2018 at 9:09
  • Because your query cannot return null - its returns an empty collection Commented Jun 13, 2018 at 9:09
  • 1
    Having an empty list is different from not having a list. Commented Jun 13, 2018 at 9:11
  • 1
    @if(Viewbag.itemlist.Any()) or @if(Viewbag.itemlist.Count > 0) Commented Jun 13, 2018 at 9: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) Commented Jun 13, 2018 at 9:17

1 Answer 1

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>
}
Sign up to request clarification or add additional context in comments.

3 Comments

the second solution does not work though...error saying does not contain a definition for 'Any''...the other solution work fine
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<T> which List<T> implements.
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<Furnitures> due to developer error at some point ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.