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

Topics: ASP.NET MVC
Sep 18, 2012 at 11:39 AM

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 ?

Coordinator
Sep 19, 2012 at 12:05 AM

Hi,

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.

Thanks,

Eilon

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

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

using

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

ends up as:

comparer="System.Collections.Generic.GenericEqualityComparer`1[System.String]" 
count
="1"
keys
="System.Collections.Generic.Dictionary`2+KeyCollection[System.String,System.Object]"
values
="System.Collections.Generic.Dictionary`2+ValueCollection[System.String,System.Object]"

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

HtmlHelper.AnonymousObjectToHtmlAttributes(tableHtmlAttributes)

Should this be filed as a bug ?

Coordinator
Sep 21, 2012 at 5: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 http://aspnetwebstack.codeplex.com/documentation on how to contribute if you like.

Sep 25, 2012 at 9: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

Coordinator
Sep 25, 2012 at 11:29 PM

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!

Thanks,

Eilon

Oct 4, 2012 at 7: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 ?