Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 733
Ian Davis Avatar asked Jul 09 '26 01:07

Ian Davis


2 Answers

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?

like image 178
Darin Dimitrov Avatar answered Jul 10 '26 14:07

Darin Dimitrov


@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.

like image 40
Ian Davis Avatar answered Jul 10 '26 15:07

Ian Davis