Sorting complex model

32 views
Skip to first unread message

Jimdy

unread,
Oct 4, 2010, 5:46:25 PM10/4/10
to mvccontrib-discuss
Hi,

I am unable to sort my model with one of its child entity's property.

Here is a short description of the problem:

In my Model:
I have a model called "Match" of type "Mentorship.Models.LINQ.Match"
Match contains a child object "Registration" of type
"Mentorship.Models.LINQ.Registration"
---------------------------------------------------------------------------------------------------------------------------------------------------
In my controller:
var matches = _dataService.FindMatchListForProgram(id);
matches = matches.OrderBy(sortManagedMatch.Column,
sortManagedMatch.Direction);
//Column has value of "Registration.UserId" and Direction has value of
"Ascending"
---------------------------------------------------------------------------------------------------------------------------------------------------
In my view:
...
Html.Grid(Model).Attributes(@class =>
"styled").Sort(ViewData["SortManagedMatch"] as
GridSortOptions).Columns(column =>
{
column.For(p =>
p.Registration.UserId).Encode(false).Named("Name").SortColumnName("Registration.UserId");

})
...
---------------------------------------------------------------------------------------------------------------------------------------------------
Error I am getting:

Could not find a property called 'Registration.UserId' on type
Mentorship.Models.LINQ.Match


Reading previous post, I thought this would work. Perhaps I am
missing something, but I am not sure how it will work after I read the
mvccontrib sorting souce code of the OrderBy function. Specifically
to the code below:

var type = typeof(T);
var property = type.GetProperty(propertyName);
if(property == null)
{
throw new InvalidOperationException(string.Format("Could not find a
property called '{0}' on type {1}", propertyName, type));
}

How does sorting of the complex model work since it will always thow
the exception when calling type.GetProperty(propertyName) on a child
property? Maybe I need to get the latest build (which I did a month
ago)?



Jeremy Skinner

unread,
Oct 5, 2010, 2:00:33 AM10/5/10
to mvccontri...@googlegroups.com
The OrderBy method in mvccontrib only supports simple scenarios where you're sorting directly on the property of your object rather than a child entity's property. If you need to do this then you'll need to use a different mechanism to perform the sort.

This is something that I'll look at adding in the future as an enhancement.

Jeremy Skinner
Sent from my iPhone

> --
> Contact Jeffrey Palermo or Eric Hexter with specific questions about the MvcContrib project. Or go to http://mvccontrib.org
>
> To unsubscribe from this group, send email to mvccontrib-disc...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/mvccontrib-discuss?hl=en

bigAPE

unread,
Oct 21, 2010, 11:47:11 PM10/21/10
to mvccontrib-discuss
Yeah, this is a bit of an issue. I have only just come across this,
but to be honest it was only during my initial testing of the
OrderBy(string, string) extension for use with the Grid and I was
using a raw entity as the source. After building a ViewModel and
mapping the object list across with a Select() and ensuring the model
was flat it's working great.

Seriously Jeremy, this is outstanding work. I can't wait to see what
you do with it next.

An Filtering field in the Column header that can be enabled per Column
with a provided DataType or using DataAnnotations on the GridModel.
Perhaps some custom Attribute that can be applied to suggest possible
filtering criteria (<, <=, >, >=, contains, starts with, etc). That
would be awesome.

Al

On Oct 5, 5:00 pm, Jeremy Skinner <jer...@jeremyskinner.co.uk> wrote:
> The OrderBy method in mvccontrib only supports simple scenarios where you're sorting directly on the property of your object rather than a child entity's property. If you need to do this then you'll need to use a different mechanism to perform the sort.
>
> This is something that I'll look at adding in the future as an enhancement.
>
> Jeremy Skinner
> Sent from my iPhone
>
> > Contact Jeffrey Palermo or Eric Hexter with specific questions about the MvcContrib project.  Or go tohttp://mvccontrib.org
Reply all
Reply to author
Forward
0 new messages