views:

668

answers:

2

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.

A: 
column.For(c => c.ID).Attributes(c=> new Dictionary<string, object>(){{"id", c.ID}});
Josh Pearce
-1: You can't share parameters between lambda expressions like that.
Jim G.
Edited: I was wrong, here is how to do an ID attribute, and I threw in a class just to show how to do that too. Sorry for the original bad info.
Josh Pearce
@Josh Pearce: You can't put 'c.ID' inside of 'Attributes()' and expect it to reference 'c' inside of 'For()'.
Jim G.
Well, this became a personal vendetta. I believe the above answer will work, finally. I am beginning to think that C# has gone from a public language to a series of personal love letter between the language designers. I mean, here is the method signature for this method: public Func<GridRowViewData<T>, IDictionary<string, object>> Attributes It just makes me feel stupid!
Josh Pearce
+1 ... I agree with Josh ... if it works .. it works ...
jalchr
@Josh Pearce: Your sample will work if you change the code slightly to this - column.For(c => c.ID).Attributes(c=> new Dictionary<string, object>(){{"id", c.Item.ID}});As the other comments have stated, you wont get direct access via previously used lambda variables due to scope. but the signature that you quote does give you access to the data itself via an Item property. This is true for the latest build of MVCContrib grid at least.
Jonathan Bates
+1  A: 

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.

Jim G.
Jim, I actually DID get it working, check my edited answer.
Josh Pearce
@Josh Pearce: I still can't even compile your answer. It would be nice if a third party had an opinion.
Jim G.