为什么我得到""" j""在代码块的开头无效。"这里?

时间:2016-04-19 22:14:18

标签: html asp.net-mvc razor view

我在我的Index.cshtml(查看)代码中得到了这个:

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

这样运行正常(在底部添加部件之前):

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

...但是当我添加基本相同的代码时:

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

...它失败并出现Parser Error。我没有看到&#34; j&#34;在它看到的代码块的开头?

这是它所暗示的行(193):

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

干扰了什么?南方?

更新

在引号内移动@符号如下:

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

......将错误消息移到了两行;现在它抱怨这条线:

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

...说,&#34; for block缺少结束&#34;}&#34;字符。确保你有匹配的&#34;}&#34;所有&#34; {&#34;这个区块内的字符,并没有&#34;}&#34;字符被解释为标记。&#34;

2 个答案:

答案 0 :(得分:1)

您没有正确地将Idvalue属性值包装在引号中。

将您的变量包装在""''

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

答案 1 :(得分:0)

Seprate Id and Value attirbute

Id =“Some”Value =“Another”