Displaying foreign key values in a grid

4 views
Skip to first unread message

ppearcy

unread,
Jun 9, 2009, 6:56:45 PM6/9/09
to mvccontrib-discuss
Hi All,
Thanks for the great extension to the MVC framework.

I have a grid I am rendering like this:
<%= Html.Grid<IndexFields>("IndexFields").Columns(column =>
{
column.For(x => x.Description);
column.For(x => x.FieldType);
})

I'd like to do something like this to display the foreign key mapping:
<% Dictionary<int, string> fieldMap = ViewData["FieldMap"] as
Dictionary<int, string>; %>
<%= Html.Grid<IndexFields>("IndexFields").Columns(column =>
{
column.For(x => fieldMap[x.FieldType]);
})

I believe that since fieldMap is not a member of IndexFields that it
isn't available to work with.

Anybody know how to accomplish this without updating the IndexFields
class?

Thanks,
Paul

Jeremy Skinner

unread,
Jun 10, 2009, 3:00:44 AM6/10/09
to mvccontri...@googlegroups.com
Hi Paul,

As far as I can see, the code that you posted should work fine. What happens when you try it?

Jeremy

2009/6/9 ppearcy <ppe...@gmail.com>

ppearcy

unread,
Jun 10, 2009, 11:25:43 AM6/10/09
to mvccontrib-discuss
Hi Jeremy,
Thanks much for the reply. Yeah, the error might be helpful :-) It
is:

CS1660: Cannot convert lambda expression to type 'string' because it
is not a delegate type


I think I might understand what is occurring. Lambdas have access to
variables within the scope that they're called. In this case, the
lambda is being passed down into the Grid<>.Columns method and it
would only have access to variables that are in scope of Grid
internally.

Let me know if this makes sense.

Really appreciate the help.

Thanks,
Paul

On Jun 10, 1:00 am, Jeremy Skinner <jer...@jeremyskinner.co.uk> wrote:
> Hi Paul,
>
> As far as I can see, the code that you posted should work fine. What happens
> when you try it?
>
> Jeremy
>
> 2009/6/9 ppearcy <ppea...@gmail.com>

Jeremy Skinner

unread,
Jun 10, 2009, 12:29:46 PM6/10/09
to mvccontri...@googlegroups.com
I can't reproduce this. I have a collection of Person objects with a Gender property (M/F) and the following test code:

<% var dictionary = new Dictionary<string, string> { { "M", "Male" }, { "F", "Female" } }; %>
<%= Html.Grid<Person>(Model).Columns(column => {
    column.For(x => dictionary[x.Gender]);
}) %>

...and this works correctly. The closure will correctly capture the dictionary variable. 

The error that you're getting looks like you're passing a lambda expression to somewhere that is expecting a string. If you like, feel free to  put together a sample project that reproduces the issue and send it to me. I'd be happy to look at it.

Jeremy

2009/6/10 ppearcy <ppe...@gmail.com>

ppearcy

unread,
Jun 10, 2009, 2:24:24 PM6/10/09
to mvccontrib-discuss
THANKS!!! Your sample was exactly what I needed. I guess I can't use a
Dictionary<int,string> and need a Dictionary<string,string>.

I'm a little baffled why, but this solves my problem.

Really appreciate the help.

Best Regards,
Paul

On Jun 10, 10:29 am, Jeremy Skinner <jer...@jeremyskinner.co.uk>
wrote:
> I can't reproduce this. I have a collection of Person objects with a Gender
> property (M/F) and the following test code:
>
> <% var dictionary = new Dictionary<string, string> { { "M", "Male" }, { "F",
> "Female" } }; %>
> <%= Html.Grid<Person>(Model).Columns(column => {
>     column.For(x => dictionary[x.Gender]);
>
> }) %>
>
> ...and this works correctly. The closure will correctly capture the
> dictionary variable.
>
> The error that you're getting looks like you're passing a lambda
> expression to somewhere that is expecting a string.
> If you like, feel free to  put together a sample project that reproduces the
> issue and send it to me. I'd be happy to look at it.
>
> Jeremy
>
> 2009/6/10 ppearcy <ppea...@gmail.com>

Jeremy Skinner

unread,
Jun 10, 2009, 2:27:37 PM6/10/09
to mvccontri...@googlegroups.com
The dictionary key would need to match the type of the FieldType property on IndexFields. So if FieldType is a string, then the dictionary would have to have string keys too. If FieldType were an integer, then the dictionary would need to have integer keys.

Jeremy

2009/6/10 ppearcy <ppe...@gmail.com>
Reply all
Reply to author
Forward
0 new messages