Lau Kondrup
Lau Kondrup

Reputation: 133

Razor exit code block

I'm trying to avoid more nesting. Is there any way to exit to the end of a @{ //code } block in Razor? I tried the example below, but it simply ignores the rest of the view then.

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

  // display products

} // I want to return to here

// Rest of the view

Upvotes: 3

Views: 2201

Answers (1)

Palanikumar
Palanikumar

Reputation: 7150

You can use @helper

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

  // display products
}



 @DisplayProducts()
//Rest of the view

Upvotes: 4

Related Questions