When using WebGrids, I find that I can access properties on the model bound to the WebGrid, but I can't access methods on the Model itself.
As an example, this works:
// Accessing a property of item works
reportGrid.Column("", header: "First Name", format: item => item.firstName )
But this does not work: (I am showing a trivial example, but for my case I must call a method on the User object itself.)
// Accessing a method on item does not work
reportGrid.Column("", header: "First Name Backwards", format: item => item.firstNameBackwards() )
=> error: 'System.Web.Helpers.WebGridRow' does not contain a definition for 'getFullName'
This seems related to the following:
Why can't I use my extension method in a delegate in the Razor WebGrid http://www.mikesdotnetting.com/Article/171/Why-You-Can%27t-Use-Extension-Methods-With-A-WebGrid
I don't see a way to apply these solutions to my problem. As Mike Brind states:
The argument that the WebGridColumn's Format parameter takes is a delegate: Func. What this means is that you have to pass in a dynamic type, and then something is done to it before it is returned as an object.
...
When you try to use an extension method, the Compiler will check the type you are trying to use it on to see if such a method exists. If it doesn't. it will check any base classes that the type derives from to see if they contain a formal method with the right name.
It seems like my method should be found, but perhaps not because the model bound to the WebGrid is actually a paging model that contains IList<T> LineItems
which hold my User references.
Is there any way to cast or get a reference to the User object itself so I can call methods defined on it in addition to accessing its properties?