I\'m experimenting a bit with several grids in asp.net mvc. Microsoft has a grid too know in the prerelease of mvc 3 so I thought I\'ll try that one out.
The basic funct
This happens because grid column names must correspond to your fields or properties. Method which generates url in grid header compares "sort" from url query string with bounded columns and with grid column name. The three of them must be the same. If definition of your column name is not configured properly, the url will not be generated correctly.
Anyway .. here is some examples of properly defined column names.
Here we have sample domain class to display
public class Person
{
public string FirstName;
public string LastName;
public Address LivesIn;
}
public class Address
{
public string Country;
public string City;
}
Now lets display List
Use your column names as fields
grid.Column("FirstName"),
grid.Column("LastName"),
grid.Column("LivesIn.City"),
grid.Column("LivesIn.Country")
... all columns has correct sorting url
If you make a typo in column name you got exception
grid.Column("FirstName"),
grid.Column("MyLastName"), <-- this throws exception
grid.Column("LivesIn.City"),
grid.Column("LivesIn.Country")
But you can use format and there will be no exception thrown
grid.Column("FirstName"),
grid.Column("MyLastName", format: item => item.LastName),
grid.Column("LivesIn.City"),
grid.Column("LivesIn.Country")
... but sorting url for column MyLastName will be BAD !!! all the time sortDir=ASC
You need to use good column name to has proper sorting url and custom format so ...
grid.Column("FirstName"),
grid.Column("LastName", format: item => item.LastName),
grid.Column("LivesIn.City"),
grid.Column("LivesIn.Country")
... everything is ok
How about complex type ?
grid.Column("FirstName"),
grid.Column("LastName"),
grid.Column("LivesIn.MyNonExistingField", format: item => item.LivesIn.City),
grid.Column("LivesIn.Country")
.... wow ... everything is ok .. that's kid of bug .. column "LivesIn.MyNonExistingField" has proper sorting url.
Ok ... What if we don't want to expose our domain structure. Then we need to add list of column names during binding
var grid = new WebGrid(persons, columnNames: new [] { "Foo" });
-- or --
grid.Bind(persons, columnNames: new [] { "Foo" });
grid.Column("Foo", format: item => item.FirstName),
grid.Column("LastName"),
grid.Column("LivesIn.MyNonExistingField", format: item => item.LivesIn.City),
grid.Column("LivesIn.Country")
.. now Foo column has proper sorting url
But careful !!! There is another bug.
If we add manual column names to binding then all column will be skipped until manual column is found. Example :
var grid = new WebGrid(persons, columnNames: new [] { "Foo" });
-- or --
grid.Bind(persons, columnNames: new [] { "Foo" });
grid.Column("FirstName"),
grid.Column("Foo", format: item => item.LastName),
grid.Column("LivesIn.MyNonExistingField", format: item => item.LivesIn.City),
grid.Column("LivesIn.Country")
... sorting url for column "FirstName" will not be generated correctly ... sortDir=ASC all the time ...to fix this add also a "FirstName" as column name like this:
var grid = new WebGrid(persons, columnNames: new [] { "FirstName", "Foo" });
-- or --
grid.Bind(persons, columnNames: new [] { "FirstName", "Foo" });
grid.Column("FirstName"),
grid.Column("Foo", format: item => item.LastName),
grid.Column("LivesIn.MyNonExistingField", format: item => item.LivesIn.City),
grid.Column("LivesIn.Country")
@Kohen
Remove columnNames in your code here
var grid = new System.Web.Helpers.WebGrid(Model.SubscriptionList,
columnNames: new List(){"Title"},
canPage:false);
... or add there all your column names like "ID", "ISP", etc