In the MVC view, {} can be used to define the attributes of an element.
However, the class attribute name in HTML conflicts with the class in C #. Therefore, you cannot use the class attribute when using it.
The solution is to add a @ symbol before the class. For example, if the class set for the element is item, you can write: {@ class = "item "}
<%: Html.TextBoxFor(model => model.Postcode, new { @class="postcode" }) %>
You can use the prefix "@" to use keywords as identifiers, which is useful when creating interfaces with other programming languages. The character @ is not an actual part of the identifier. Therefore, this identifier may be considered as a normal identifier without a prefix in other languages. The identifier with the @ prefix is calledVerbatim identifier(Verbatim identifier ). Allow @ prefixes to be used for non-Keyword identifiers, but (in the sense of code writing style) it is strongly recommended not to do so.
Example:
class @class{ public static void @static(bool @bool) { if (@bool) System.Console.WriteLine("true"); else System.Console.WriteLine("false"); }}class Class1{ static void M() { cl\u0061ss.st\u0061tic(true); }}
Define a class named "class", which has a static method named "static". This method carries a parameter named "bool. Note that because Unicode escape characters are not allowed in keywords, the mark "Cl \ u0061ss" is an identifier, which is the same as the "@ Class" identifier.
In MVC, in addition to @, the problem can also be solved in this way.
<%: Html.TextBoxFor(model => model.Postcode, new Dictionary<string, object> { { "class", "postcode" } }) %>