Why am I getting """j"" is not valid at the start of a code block." here?

I've got this in my Index.cshtml (View) code:

<div class="col-md-6">
    <label class="control-label">Delivery Performance (report spans a number of days)</label>
    <br>
    <label>From</label>
    <select>
        @for (int i = 1; i <= @maxDaysBackForDeliveryPerformance; i++)
        {
            <option id="selItem_@(i) value=@"i">@i</option>
        }
    </select>
    <label>days back</label>
    <br>
    <label>To</label>
    <select>
        @for (int j = 1; j <= @maxDaysBackForDeliveryPerformance; j++)
        {
            <option id="selItem_@(j) value=@"j">@j</option>
        }
    </select>
    <label>days back</label>
    <br>
    <button id="btnTestDeliveryPerformanceSettings">Test Settings</button>
</div>

This ran fine (prior to adding the part at the bottom):

<div class="col-md-6">
    <label class="control-label">Delivery Performance (report spans a number of days)</label>
    <br>
    <label>From</label>
    <select>
        @for (int i = 1; i <= @maxDaysBackForDeliveryPerformance; i++)
        {
            <option id="selItem_@(i) value=@"i">@i</option>
        }
    </select>
    <label>days back</label>
</div>

...but when I added the essentially identical code:

<br>
<label>To</label>
<select>
    @for (int j = 1; j <= @maxDaysBackForDeliveryPerformance; j++)
    {
        <option id="selItem_@(j) value=@"j">@j</option>
    }
</select>
<label>days back</label>
<br>
<button id="btnTestDeliveryPerformanceSettings">Test Settings</button>

...it fails with that Parser Error. I don't see the "j" at the start of the code block that it sees?

This is the line (193) it implicates:

<option id="selItem_@(j) value=@"j">@j</option>

What has gone haywire/south?

UPDATE

Shifting the @ sign inside the quotes like so:

<option id="selItem_@(j) value="@j">@j</option>

...moved the err msg up two lines; now it's complaining about this line:

@for (int j = 1; j <= @maxDaysBackForDeliveryPerformance; j++)

...saying that, "The for block is missing a closing "}" character. Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup."

Upvotes: 0

Views: 243

Answers (2)

user5733654
user5733654

Reputation:

Seprate Id and Value attirbute

Id = "Some" Value = "Another"

Upvotes: 0

Shyju
Shyju

Reputation: 218722

You are not properly wrapping the Id and value attribute values in quotes.

wrap your variable in "" or ''.

@for (int i = 1; i <= maxDaysBackForDeliveryPerformance; i++)
{
     <option id="selItem_@(i)" value="@i">@i</option>
}

Upvotes: 1

Related Questions