C#, .Net, MVC, jQuery, JavaScript, HTML, CSS, Umbraco, Phonegap / Cordova, Xamarin, iPhone, iOS, Swift, Android, Windows, apps, websites, Full Stack, ecommerce, Facebook, responsive, front-end. Freelance developer, programmer for web and mobile in Bristol, UK.
Saturday, 2 June 2012
Using hyphenated data attributes in MVC
In .net MVC this doesn't work:
@Html.TextBox("name", null, new { @class = "input-xlarge", @data-input = "75"})
The hyphenated 'data-input' attribute causes the error "Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access."
Well, as of MVC 3, you can actually use an underscore instead, like so:
@Html.TextBox("name", null, new { @class = "input-xlarge", @data_input = "75"})
And it'll handle it for you, converting it to a hyphen. It knows you want a hyphen rather than an underscore as underscores aren't valid in html attributes. Lucky huh?
Thanks Ed. It's these little things that are often the difficult things to figure out. Your post saved me a fair bit of time wondering why my hyphens were causing such a problem. Cheers,
Ed Pitt - I'm a software engineer based in Bristol, which is in South West England, sort of near Bath and Cardiff :) Oh, and did I say I'm a freelance developer in Bristol, South West? And a freelance mobile developer in Bristol, South West? And I do C#.NET, MVC, Umbraco, Xamarin for Android and Windows Phones, iOS development, in Bristol, that's in the South West? And does this text sound like it's here just for SEO?
Thanks Ed. It's these little things that are often the difficult things to figure out. Your post saved me a fair bit of time wondering why my hyphens were causing such a problem. Cheers,
ReplyDeleteJamie