-
Notifications
You must be signed in to change notification settings - Fork 225
if block inside foreach block doesn't parse ending brackets correctly #679
Comments
Does this work at runtime for you or is it purely a tooling issue? |
Fails in both places - didn't really notice it in tooling until it failed while trying to run. |
additional info: I thought I would outsmart it by putting two single-line if statements with the code to execute on the same line and no {block}. This time it is even stranger:
This code executes, but not as expected, and the tooling parser is strange too. a> at the bottom notice that there is an if {block} but it doesn't break things. I can't figure out the difference, but it works here while a block towards the top, as in the previous example, breaks (in runtime and tooling) b> In this example, if I do NOT put brackets around block for the last if, I get a really nice error warning in Visual Studio that single-statement control flow statements are not allowed in cshtml and you have to use brackets. That makes sense, BUT, I don't get the same warning with the two if statements above. So what is the difference? c> When this page executes, in my sample dataset there is only one clinic, so when this runs you'd expect the first if statement to evaluate true and update the debugger sees both sides of the conditional as strings and you can clearly see they are the same, but the != evals to true for the second if while evaluating false for the first one! Maybe issue#c is due to the lack of brackets and isn't supported, but hopefully all this info helps you track it down. |
@NTaylorMullen can you see if there's a safe fix for this? |
So I played around with this and it turns out your @{
foreach (int x in new[] { 1, 2, 3 })
{
var val = @x;
if (val != 3) { }
}
} I'll work on getting a fix for this |
- We used to accept until markup or an ending brace which didn't allow the parser to balance nested braces. #679
I resolved by replacing the if statement with a switch statement in NET framework 4.6.1 |
I think this is different from #51 but adding the link in case there is a relation.
Using RC2-16770
My view iterates through a collection of items (Practitioners) and displays some things. That works great UNTIL I add an if block inside the foreach. Then razor finds the closing bracket for the foreach, but associates it to the parent bracket enclosing the overall razor code block. This happens both in VS2015 code highlighting and when the code is executed. Also note, I can add an if statement with a single line - it is when I add the brackets to designate a code block that the issue occurs.
Works:
Fails:
the only difference is the insertion of the if block inside the for each:
Tried to distill this to the simplest possible:
It seems you have to have at least one line of code with razor sytax in it prior to the if block or it will parse correctly.
The text was updated successfully, but these errors were encountered: