Devin Goble
Devin Goble

Reputation: 2877

foreach contents getting skipped

I've got an ASP.NET MVC4 view that looks like this:

@model IEnumerable<Bloom.Models.GalleryImageVM>
<div id="gallery">
    @foreach (var img in Model)
    {
        <a href="#" id="@img.Title">
            <img src="@img.URL" />
        </a>
    }
</div>

GalleryImageVM is an object with two string properties:

public class GalleryImageVM
{
    public string URL { get; set; }
    public string Title { get; set; }
}

Data is definitely getting populated since Model has the expected values in it. However, nothing inside the foreach gets hit. A breakpoint won't get hit, and if I step through it, the execution will touch each of the expected parts of the foreach definition, but won't enter the curly braces.

What am I missing?

Update: Just to clarify, Model most definitely has data in it, and stepping through the code shows that both Model and img have the expected result.

Upvotes: 0

Views: 112

Answers (1)

balexandre
balexandre

Reputation: 75133

the best way to see it is to debug the output...

how about if you do things like:

<div id="gallery">
    @if(Model.Count() == 0)
    {
       <h2>Nothing to see here</h2>
    }
    else 
    {
        foreach (var img in Model)
        {
          <a href="#" id="@img.Title">
            <img src="@img.URL" />
          </a>
        }
    }
</div>

or as simple as

<div id="gallery" class="[email protected]()">
    @foreach (var img in Model)
    {
        <a href="#" id="@img.Title">
            <img src="@img.URL" />
        </a>
    }
</div>

and you can even hit "Debug" and add a breakpoint before the return View(model) and see the contents of your model before it sends the data to the view.

enter image description here

Upvotes: 1

Related Questions