Matthew
Matthew

Reputation: 11623

MVC Razor brackets inside code block

I'm having issues do linq queries inside of a code block.

@{
  var foo = @Model.Things.Select((value, index) => new { value, index });
}

The problem is the new{}, it sees the second bracket as closing the entire code block. Any way to escape it?

Upvotes: 7

Views: 6536

Answers (2)

eouw0o83hf
eouw0o83hf

Reputation: 9598

Remove the @ from Model:

@{
  var foo = Model.Things.Select((value, index) => new { value, index });
}

Upvotes: 14

petro.sidlovskyy
petro.sidlovskyy

Reputation: 5103

Please try

@{
 var foo = Model.Things.Select((value, index) => new { value, index });
}

Upvotes: 5

Related Questions