Question

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.

Was it helpful?

Solution 3

Perhaps the best way to render the specified HTML would be to abandon the MVCContrib HTML.Grid entirely and just render the markup with a foreach.

OTHER TIPS

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.

column.For(c => c.ID).Attributes(c=> new Dictionary<string, object>(){{"id", c.ID}});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top