0

I am working on a project that declares html attributes as @class = "className" or @id = "ID" however for this form control it does not happen:

@Html.CheckBoxFor(Model => Model.IsEBSCreated, new { disabled = "disabled" })

Questions:

  1. Why does the HTML attribute disabled not have an @ symbol before it?
  2. Is the @ symbol not always required?
  3. Does the HTML attribute checked require an @ symbol?

Thank you

4
  • 2
    The @ is used to escape names that would otherwise be C# language keywords (class, checked, bool, abstract etc). Commented Aug 10, 2022 at 11:57
  • 1
    You should use m instead of Model inside HtmlHelper methods because Model is the same name as this.Model inside a View - name collisions should be avoided. Commented Aug 10, 2022 at 12:00
  • @MartinCostello I see, in that case id does not require an @ But in the code it always has an @ so I assume it doesn't break anything if its not necessary but present Commented Aug 10, 2022 at 12:00
  • @Dai Thanks for the advice that definitely makes sense! I'll change that Commented Aug 10, 2022 at 12:02

1 Answer 1

2

Why does the HTML attribute disabled not have an @ symbol before it?

Because disabled is not a C# language keyword.

Is the @ symbol not always required?

Only when you're using a C# language keyword as an identifier.

Does the HTML attribute checked require an @ symbol?

Yes, because checked (and unchecked) are C# language keywords.


For example:

public interface @interface
{
    string? @string { get; }
}

public class @class : @interface
{
    private static readonly string? @null = null;
    string? @interface.@string => @null;
}

Feel free to use the above code in a team project if you happen to dislike a particular member of your team.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.