Tuesday, February 15, 2011

Why to add @ symbol for class in HTML extenstion control in ASP.NET MVC


                    There is a small difficulty you feel when you are working
HTML helper methods.You can not add  some of properties directly to the HTML helper methods.
ex: class

                     The problem is here  C# compiler doesn’t expect you to use C# reserved words as property names. So, if you try to render a class attribute by passing new { class = "myCssClass" }, you’ll get a compiler error (class is a reserved word in C#). To avoid this problem, prefix any C# reserved words with an @ symbol (e.g., write new { @class =
"myCssClass" }). That tells the compiler not to treat it as a keyword. The @ symbol disappears during compilation(as it’s just a hint to the compiler), so the attribute will be rendered simply as class.

<%
   1: = Html.TextBox("LastName", Model.LastName, new { @class = "width-600" })
%>

No comments:

Post a Comment