Ian Davis
Ian Davis

Reputation: 19443

razor variable does not exist

    @for(int i = 0; i < this.Model.PresetReports.Count; i++) {
        @{ var preset = this.Model.PresetReports.ElementAt(i); }
        <a href="#" class="@(i == 0 ? "selected" : string.Empty)">@preset.Label</a>
    }

It says that 'preset' does not exist in the current context. ?? Thanks!

Upvotes: 2

Views: 2315

Answers (2)

Ian Davis
Ian Davis

Reputation: 19443

@for(int i = 0; i < this.Model.PresetReports.Count; i++) {
    var preset = this.Model.PresetReports.ElementAt(i);
    <a href="#" class="@preset.class">@preset.Label</a>
}

That does it.

Upvotes: 3

Darin Dimitrov
Darin Dimitrov

Reputation: 1039538

Try like this:

@for(int i = 0; i < this.Model.PresetReports.Count; i++) 
{
    var preset = this.Model.PresetReports.ElementAt(i);
    @<a href="#" class="@preset.class">@preset.Label</a>
}

but I really don't see why you wouldn't use a foreach loop which would make a little more sense in your scenario:

@foreach (var preset in Model.PresetReports)
{
    @<a href="#" class="@preset.class">@preset.Label</a>
}

Now this being said I have some doubts about preset.class. You really have a property called class (with lowercase c which is a reserved word in C#) on your view model?

Upvotes: 6

Related Questions