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

Need clarification on using @ prefix on Razor component's attribute values #34564

Closed
nicholasyin opened this issue Jan 25, 2025 · 3 comments · Fixed by #34570
Closed

Need clarification on using @ prefix on Razor component's attribute values #34564

nicholasyin opened this issue Jan 25, 2025 · 3 comments · Fixed by #34570

Comments

@nicholasyin
Copy link

nicholasyin commented Jan 25, 2025

Description

Image

For one thing, the example below doesn't seem to follow the above convention of not using @ prefix for non-literal variable in attribute values.
In addition, I think adding @ is more reasonable because Count="ct" would intuitively give me the impression that ct is a regular string assigned to Count variable.

Page URL

https://learn.microsoft.com/en-us/aspnet/core/blazor/components/?view=aspnetcore-9.0#route-parameters

Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/blazor/components/index.md

Document ID

31ad18df-1904-bbdd-cfed-8d0d0267c10e

Article author

@guardrex

Metadata

  • ID: 81a32115-33f8-7d06-aaf0-b8ce918b5527
  • Service: aspnet-core
  • Sub-service: blazor

Related Issues

Copy link
Contributor

💃🕺🥳 Happy New Year! 🎈🎆🍾🥂🎉

Stand-by! ... A green dinosaur 🦖 will be along shortly to assist.

@guardrex
Copy link
Collaborator

guardrex commented Jan 25, 2025

Thanks for mentioning this, @nicholasyin.

We did make a change a couple of years ago, telling devs that we wouldn't generally prefix with @ when it wasn't required for component parameter values. Before that, yes, we did always use @ in these examples, as the Parameter2 component shows.

WRT ...

I think adding @ is more reasonable because Count="ct" would intuitively give me the impression that ct is a regular string assigned to Count variable.

That's going to end up being a personal (or organizational) preference on your end. Management (Dan Roth) has decided that we'll not do it in framework test code and documentation examples. Of course, you're welcome to do it in your own apps. The usual recommendation applies: We'd simply suggest that devs be consistent in their choice within an app and even across the apps of their organization.

I'll be back in the office ... well ... at my desk 😆 (I'm remote) ... on Monday morning. I'll work on this first thing and resolve it, including adding some context to the article on the subject. I'll ping u on the PR when it goes up.

Thanks for the issue! Have a great weekend and Happy New Year! 💃🕺🥳🍾

@guardrex guardrex moved this from Triage to P0/P1 - High Priority in Blazor.Docs Jan 25, 2025
@guardrex
Copy link
Collaborator

Good morning, @nicholasyin ... I just took a closer look.

wouldn't generally prefix with @ when it wasn't required for component parameter values

That's correct, and the example is correct. This has to do with the type that is assigned to in the ParameterChild component. The type is a string for Title. Therefore, these assignments in the parent to the instance of the component via its component parameters must be prefixed with @. Otherwise, Blazor thinks that the value passed in the component parameter attribute value should be a string literal.

I agree with you that the current text isn't good enough to describe this. I'm going to make some updates right now to clear this up. I'll ping u on the PR shortly. Thanks for the issue! ... and Happy New Year! 🕺💃🥳🍾

@guardrex guardrex moved this from P0/P1 - High Priority to In progress in Blazor.Docs Jan 27, 2025
@github-project-automation github-project-automation bot moved this from In progress to Done in Blazor.Docs Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants