剃刀退出代码块

时间:2016-06-01 12:06:28

标签: c# asp.net asp.net-mvc razor

我正在努力避免更多的嵌套。 有没有办法退出到Razor的@{ //code }区块的末尾? 我尝试了下面的例子,但它只是忽略了视图的其余部分。

@{
  if(Model.Products.Count == 0)
  {
    <p>No products were found</p>
    return;
  }

  // display products

} // I want to return to here

// Rest of the view

2 个答案:

答案 0 :(得分:3)

您可以使用@helper

@helper DisplayProducts()
{
    if(Model.Products.Count == 0)
    {
        <p>No products were found</p>
        return;
    }

  // display products
}



 @DisplayProducts()
//Rest of the view

答案 1 :(得分:-1)

你可以用不同的方式写出来:

@if(Model.Products.Count == 0)
{
   <p>No products were found</p>
}
else
{
 //display products...
}