B. Clay Shannon B. Clay Shannon - 7 months ago 22
HTML Question

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

Answer

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>
}
Comments