Friday, 30 December 2011

Problem setting an html attribute containing hyphens in ASP.NET MVC

C# doen't allow you to have a hyphen in the name of a property. For example if you try something like


@Html.TextBox("firstname", "", new { id = "firstname", maxlength = 100, data-val="true", data-val-required="Please enter the first name of the contact." })


It will fail to run.

Try using underscores which should be automatically converted into dashes by the standard HTML helpers:


@Html.TextBox("firstname", "", new { id = "firstname", maxlength = 100, data_val="true", data_val_required="Please enter the first name of the contact." })

No comments:

Post a Comment