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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With