Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anchor tag helper is broken within foreach loop #11327

Open
SoftCircuits opened this issue Dec 20, 2024 · 5 comments
Open

Anchor tag helper is broken within foreach loop #11327

SoftCircuits opened this issue Dec 20, 2024 · 5 comments
Labels
area-compiler Umbrella for all compiler issues untriaged

Comments

@SoftCircuits
Copy link

SoftCircuits commented Dec 20, 2024

I have a foreach loop that was working, but now it isn't. Neither the a or img tag helpers are working (neither in Intellisense or when I render the actual HTML). You can see in the image below that attributes like asp-page and asp-route-it do not show tag helper highlighting.

Image

If I remove the @ from my foreach, so that it is not a C# loop, then the tag helpers look as they should look.

Image

I've spent considerable time looking for some sort of syntax error or something that might be causing this to happen, but I've found nothing.

Can anyone else suggest what to look for?

Below is what the generated HTML looks like. Note the ~ in the image path and the unmodified asp-page and asp-route-id attributes.

<tr>
    <td>NGL</td>
    <td>21</td>
    <td>91292A</td>
    <td>Propane</td>
    <td>Propane</td>
    <td>43,660</td>
    <td>12/9/24 9:07 AM (11 days)</td>
    <td class="text-end">
        <a class="add-transfer" href="javascript:void(0)" data-id="161161"><img src="/images/add.png" title="Add Product Transfer" /></a>
        <a asp-page="TruckImages" asp-route-id="161161">
            <img src="~/images/images_flag.png" title="Images (Has Existing Images)" role="button" />
        </a>
        <a asp-page="ArriveTruck" asp-route-id="161161"><img src="/images/edit.png" title="Edit" /></a>
        <a asp-page="DepartTruck" asp-route-id="161161"><img src="/images/depart.png" title="Depart Truck" /></a>
    </td>
</tr>
@davidwengier
Copy link
Contributor

I suspect it's the collection expression. Sadly Razor can not be assumed to support all C# language features from the get go. There is work being done in this area, though.

Does removing ?? [] from the end of the foreach fix it?

@davidwengier davidwengier added the area-compiler Umbrella for all compiler issues label Dec 20, 2024
@SoftCircuits
Copy link
Author

SoftCircuits commented Dec 20, 2024

@davidwengier: Thanks for responding. No, that makes no difference. In fact, I didn't have that part when the issue initially occurred.

In fact, I tried a simple for loop,

@for (int i = 0; i < 100; i++)

And the tag helpers are still not recognized.

I even tried declaring truck within the loop so there are no errors, and the tag helpers are still not recognized.

@davidwengier
Copy link
Contributor

Does it happen in an otherwise empty .cshtml file? Maybe there is something above the @foreach in your original image that is throwing the parser out?

Certainly doesn't repro for me if I just try it in a simple file:
Image

If you can share the whole file, or simplify it down to a small repro file/project, that will hopefully indicate what is going on.

@SoftCircuits
Copy link
Author

SoftCircuits commented Dec 20, 2024

@davidwengier I was finally able to find an error in my markup.

It was the following block of markup, which was above the code we discussed. Note the two double quotes at the end of the form-check form-switch <div>.

<div class="d-flex justify-content-between" title="Automatically refresh this page every 5 minutes">
    <div>
        <h4>@ViewData["Title"] (@truckCount)</h4>
    </div>
    <div>
        <div class="form-check form-switch"">
            <input class="form-check-input" type="checkbox" id="auto-refresh">
            <label class="form-check-label" for="auto-refresh">Auto Refresh</label>
        </div>
    </div>
</div>

Okay, would be nice if the editor could flag this somehow, but it's due to my error. Apologies, and thanks for looking.

@davidwengier
Copy link
Contributor

Good work! Agree, a diagnostic here would be good. Will leave this issue open for the compiler team to review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-compiler Umbrella for all compiler issues untriaged
Projects
None yet
Development

No branches or pull requests

2 participants