Search code examples
c#asp.net-mvcformsrazorhtml-helper

Do All HTML attributes require a @ in ASP.NET MVC


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


Solution

  • 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.