htmlattributes on webgrid do not supper html5 style attributes (data-*)

Sep 18, 2012 at 12:39 PM

I've been working on a new project with mvc4.

I wanted to use the webgrid helper for showing data.

The table tag needed some extra data for javascript so i wanted to put these in a "data-link" attribute.

@grid.GetHtml( htmlAttribtues: new {data-link: "4"} ) (and the columns ofcourse)

Didn't work. This style of adding attributes work on all types of helpers ( editorfor, labelfor, etc... )

What would be the best way to get this working ?

Sep 19, 2012 at 1:05 AM


You should be able to used an underscore instead of a dash and the helpers will automatically convert it. For example:

@grid.GetHtml( htmlAttribtues: new {data_link: "4"} )

The reason for this is that a dash ("-") isn't a valid C# identifier, so by using an underscore, you avoid that problem. Then the helper auto-converts underscores to dashes (because it's very unlikely you want to use underscores anyway).

The alternative is that you can pass in a dictionary of attributes and that way you can pass in whatever you want and the helper won't re-format any of the data or values.



Sep 20, 2012 at 11:08 AM
Edited Sep 20, 2012 at 11:11 AM

I have tried "data_link" that doesn't work.. It shows up as "data_link" it doesn't get translated


htmlAttributes: new Dictionary<StringObject> {{"data-link""4"}}

ends up as:


I've been going through the source code and I've spotted the place (I think) where where the attributes get used.

system.web.helpers \ WebGrid \ _WebGridRenderer.cshtml

 var htmlAttributeDictionary = TypeHelper.ObjectToDictionary(htmlAttributes);

Cannot find what objecttodictionary does.. I know some other helpers use


Should this be filed as a bug ?

Sep 21, 2012 at 6:33 PM

Oh, oops! Yes that is certainly a bug. It looks like there are two different ways that the code converts objects to dictionaries, and this code is using the wrong way. Also, if you're interested, this looks like it could be a fun bug to submit a pull request for. We have info on on how to contribute if you like.

Sep 25, 2012 at 10:22 AM

That would be fun to do, but I'm still using windows 7 with visual studio 2010 so that will be a problem..

I will take a look at filing a bug

Sep 26, 2012 at 12:29 AM

Hi there,

We're actually taking a look right now at making the build system be a lot more friendly to Win7 + VS2010, so hopefully this won't be a blocker for very long!



Oct 4, 2012 at 8:25 AM

We've been able to setup a windows server 2012 with visual studio 2012.

Been working on getting the "build" script to run and complete with all unit tests run succesfully..

But I am having trouble with this.. what would be the best place to find some help with this ?