I suggest you ...

Model Binding should support sort expressions containing dots

The following example code illustrates a limitation in the sorting support for model binding using GridView (and other list controls). Sorting by the Name field on product works fine. However, sorting by Category.Name fails with exception "Instance property 'Category.Name' is not defined for type 'WebApplication7.Product'". However, although sorting doesn't work, data binding does. I think this inconsistent behavior should be addressed.

Default.aspx

<asp:GridView ID="GridView1" runat="server" ItemType="WebApplication7.Product" SelectMethod="GridView1_GetData"
AutoGenerateColumns="false" AllowSorting="true">
<Columns>
<asp:BoundField HeaderText="Name" DataField="Name" SortExpression="Name" />
<asp:BoundField HeaderText="Category" DataField="Category.Name" SortExpression="Category.Name" />
</Columns>
</asp:GridView>

Default.aspx.cs

public class Product
{
public string Name { get; set; }
public Category Category { get; set; }
}

public class Category
{
public string Name { get; set; }
}

public IQueryable<Product> GridView1_GetData()
{
return new Product[]
{
new Product { Name = "Product 1", Category = new Category { Name = "Category 2" } },
new Product { Name = "Product 2", Category = new Category { Name = "Category 1" } }
}.AsQueryable();
}

23 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Jon MillerJon Miller shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    2 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • Jon MillerJon Miller commented  ·   ·  Flag as inappropriate

        Thanks much for implementing this. It's nice to see an update to Web Forms. Now, if only it were brought forward into the new ASP.NET.

      • AlexAlex commented  ·   ·  Flag as inappropriate

        I back this up, especially, since sorting is working properly with dot attributes with GridView and SqlDataSource control.

      Feedback and Knowledge Base