Asp.net mvc 当'@';没有签名?
在一个视图文件中,我添加了以下代码块。(为了澄清这一点,我省略了不必要的代码块。)Asp.net mvc 当'@';没有签名?,asp.net-mvc,razor,Asp.net Mvc,Razor,在一个视图文件中,我添加了以下代码块。(为了澄清这一点,我省略了不必要的代码块。) @对于(int i=0;iModel.Step1.Locations[i].City); TextBoxFor(m=>Model.Step1.Locations[i].Country); } 这不会显示上面代码块的文本框,尽管在调试时迭代可以正常工作 然后我改变了上面的代码块,如下所示 <div> <table>
@对于(int i=0;iModel.Step1.Locations[i].City);
TextBoxFor(m=>Model.Step1.Locations[i].Country);
}
这不会显示上面代码块的文本框,尽管在调试时迭代可以正常工作
然后我改变了上面的代码块,如下所示
<div>
<table>
@for (int i = 0; i < Model.Step1.Locations.Count; i++)
{
<tr>
<td>
@Html.TextBoxFor(modelItem => Model.Step1.Locations[i].City)
</td>
<td>
@Html.TextBoxFor(m => Model.Step1.Locations[i].Country)
</td>
</tr>
}
</table>
</div>
@对于(int i=0;iModel.Step1.Locations[i].City)
@Html.TextBoxFor(m=>Model.Step1.Locations[i].Country)
}
这很有效。
我们必须显式添加“@”符号吗?
因为在第一个代码块中,这些HTMLHelper被用在“for”循环的花括号内,所以我认为不需要“@”符号
你能解释一下这背后的事实吗
谢谢。使用
@
符号表示“如果此代码的结果返回某些内容,请将其输出。”
如果您不将
@
放在前面,代码将执行,但不会呈现任何内容。您应该首先阅读Razor的工作原理,以及它如何解释HTML代码和C代码的位置。请浏览一次
<div>
<table>
@for (int i = 0; i < Model.Step1.Locations.Count; i++)
{
<tr>
<td>
@Html.TextBoxFor(modelItem => Model.Step1.Locations[i].City)
</td>
<td>
@Html.TextBoxFor(m => Model.Step1.Locations[i].Country)
</td>
</tr>
}
</table>
</div>