Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

if block inside foreach block doesn't parse ending brackets correctly #679

@esargent

Description

@esargent

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:

 @model DistributorPortalViewModel
 <div class="">
 <h1>Simple Customer List</h1>
 <div>Click a name to expand for details.</div>
 <hr />
@{
    string target, targetId, currentClinic = "";
    foreach (Practitioner p in Model.Practitioners)
    {
        targetId = "collapseArea" + @p.Id;
        target = "#" + targetId;
        <div class="m5">
            <a data-toggle="collapse" href=@target>@Html.DisplayFor(modelItem => p.LastName)</a>
            <div id=@targetId class="collapse">
                @Html.DisplayFor(modelItem => p.Name) at @Html.DisplayFor(modelItem => p.PrimaryClinic.Name)<br />
                Is Listed in Directory? @Html.DisplayTextFor(modelItem => p.IsListed)
            </div>
        </div>
}
}
<hr />

Fails:

@model DistributorPortalViewModel
<div class="">
<h1>Simple Customer List</h1>
<div>Click a name to expand for details.</div>
<hr />
@{
    string target, targetId, currentClinic = "";
    foreach (Practitioner p in Model.Practitioners)
    {
        targetId = "collapseArea" + @p.Id;
        target = "#" + targetId;
        if (currentClinic != p.PrimaryClinic.Name)
        {
            currentClinic = p.PrimaryClinic.Name;
            <h2>@p.PrimaryClinic.Name</h2>
            }
        <div class="m5">
            <a data-toggle="collapse" href=@target>@Html.DisplayFor(modelItem => p.LastName)</a>
            <div id=@targetId class="collapse">
                @Html.DisplayFor(modelItem => p.Name) at @Html.DisplayFor(modelItem => p.PrimaryClinic.Name)<br />
                Is Listed in Directory? @Html.DisplayTextFor(modelItem => p.IsListed)
            </div>
        </div>
}
}
<hr />

the only difference is the insertion of the if block inside the for each:

            if (currentClinic != p.PrimaryClinic.Name)
        {
            currentClinic = p.PrimaryClinic.Name;
            <h2>@p.PrimaryClinic.Name</h2>
            }

Tried to distill this to the simplest possible:

@model DistributorPortalViewModel
@{
    string targetId, c = "";
    foreach (var p in Model.Practitioners)
    {
        targetId = @p.Name;
        if (c != p.Name)
        {
            <div>something</div>
        }
    }
}

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions