使用RazorViewEngine,我可以这样做:
if (somecondition) {
<div> some stuff </div>
}
Run Code Online (Sandbox Code Playgroud)
但我似乎无法做到这一点(Razor感到困惑):
if (somecondition) {
<div>
}
if (someothercondition) {
</div>
}
Run Code Online (Sandbox Code Playgroud)
我有一种情况需要将我的开始和结束html标签放在不同的代码块中 - 我怎样才能在Razor中执行此操作?
没有太多运气,我在Razor中有以下if/else语句,效果很好
<small>
@if(deletedView){
@:Deleted
}
else {
@:Created
} by
</small>
Run Code Online (Sandbox Code Playgroud)
我想做这样的事情:
<small>
@(deletedView) ? @:Deleted : @:Created by
</small>
Run Code Online (Sandbox Code Playgroud)
但那失败了.什么是正确的语法?
处理这样的事情的最佳方法是什么:
剃刀代码:
@if(!disableRowDiv)
{
<div class="row">
}
<div>some content here</div>
@if(!disableRowDiv)
{
</div>
}
Run Code Online (Sandbox Code Playgroud)
这样Razor引擎就不会产生这个错误:
分析器错误消息:
if块缺少结束"}"字符.确保此块中的所有"{"字符都有匹配的"}"字符,并且没有任何"}"字符被解释为标记.
我在MVC中使用Razor语法进行简单的for循环:
@for (int i = 0; i < Model.ProductViewModels.Count; i++)
{
if (i%2 == 0)
{
<div class="row">
}
<div class="col-md-4">
<a href="/product?id=@Model.ProductViewModels[i].Id">@Model.ProductViewModels[i].Title - @Model.ProductViewModels[i].Isbn13
<br />
<img src="@Model.ProductViewModels[i].ImageUrl" />
</a>
</div>
@if (i%2 == 0)
{
</div>
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎是我心目中的合法代码,但它不起作用!
我收到错误:
Meddelelse om parserfejl: 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 …Run Code Online (Sandbox Code Playgroud)