MVCContrib, Html.Grid: How can I bind a row-based identifier to a td tag?

Here is my current view code:

<% Html.Grid((List<ColumnDefinition>)ViewData["Parameters"]) .Columns(column => { column.For(c => c.ID); column.For(c => c.Name); }).Render(); %> 

I would like to attach the "id" HTML attribute to each td "name" tag as such:

 <table class="grid"> <thead> <tr> <th>Id</th> <th>Name</th> </tr> </thead> <tbody> <tr class="gridrow"> <td>1</td> <td id="parameter_1">Address</td> </tr> <tr class="gridrow_alternate"> <td>2</td> <td id="parameter_2">Phone Number</td> </tr> </tbody> </table> 

My question is:

How to do it?

I looked at the Attributes extension method, but I was not sure how I could get it to work.

+4
source share
3 answers

Perhaps the best way to render the specified HTML is to completely abandon MVCContrib HTML.Grid and simply render the markup using foreach.

+1
source

The syntax is rather strange, but it works. Josh's answer is on the right track. Here is the complete answer using a line from my current project. This includes syntax for using several attributes:

 col.For(ts => ts.Subtitle.Abbreviation) .Named("Subtitle<br />Language") .Attributes(x => new Dictionary<string, object>() { // { "name", "value" }; results in: { "title", x.Item.Subtitle.Text }, // title="someLanguage" { "class", "someCssClass" }, // class="someCssClass" { "id", "someIdOrOther' } // id="someIdOrOther" }); 

You can include as many name / value pairs as you want. Each of them will have access to the .Item property for the lambda variable ( x in the above example) if you need to use data from this row object.

+4
source
 column.For(c => c.ID).Attributes(c=> new Dictionary<string, object>(){{"id", c.ID}}); 
+3
source

All Articles