Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Here's my current view code:

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

I'd like to attach an HTML "id" attribute to each "name" td 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:

How do I do this?

I considered the 'Attributes' extension method, but I wasn't sure how I could make it work.

like image 236
Jim G. Avatar asked Feb 28 '23 01:02

Jim G.


1 Answers

The syntax is rather bizarre, but it does work. Josh's answer is on the right track. Here's the full answer, using a line from my current project. This includes the syntax to use more than one attribute:

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 will have access to the .Item property on the lambda variable (x in the above example) in case you need to use data from that row's object.

like image 185
Ryan Lundy Avatar answered Apr 28 '23 10:04

Ryan Lundy