Hey Ya’ll,
Well I was thinking of an ill-tricky way to append a string to the beginning of each list item in ASP.NET MVC’s SelectList collection, after thinking:
new SelectList(ViewData.Model.Formats.ForEach(x => x.Name = index + " - " + x.Name), "ID", "Name");
pausing (doh! – and not to mention not knowing how I would have gotten index incrementing..)
“Argument type ‘void’ is not assignable to parameter type ‘System.Collections.IEnumerable”
and then thinking again (erm. With help from some buddies over at Stack Overflow)..
I ended up with a method that was exactly what I needed! This is a sweet use of anonymous methods I find. I’m sure this approach will come in handy for a lot of things in the future; hardly ever use lambda Select()!
new SelectList(ViewData.Model.MessageTypeFieldFormats.Select((x, i) => new { ID = x.ID, Name = (i + 1) + " - " + x.Name })
This produces the desired output as…
1 – first entry
2 – second entry
3 – third entry
and so on…. have a nice day =:)
2 Trackbacks/Pingbacks
[...] to VoteSweet lambda trick to append to MVC SelectList() (1/8/2009)Thursday, January 08, 2009 from Graham O’NealeHey Ya’ll, Well I was thinking of an ill-tricky way [...]
Sweet lambda trick to append to MVC SelectList() « {Programming} & Life…
Thank you for submitting this cool story – Trackback from DotNetShoutout…
Post a Comment