GridMVC -- throw error on sorting the Nullable Foreign Key field

Apr 22, 2015 at 1:01 PM
Hi All,

One entity Student has address as nullable foreign key.. model is like
  class Student
  {
    public int Id {get; set;}
    public string Name {get; set;}

    public int? AddressId {get; set;}
    [ForeignKey("AddressId")]
    public Address Address {get; set;}
  }

  class Address
  {
    public int Id {get; set;}
    public string City {get; set;}
    public string Country{get; set;}
  }

now in GridMVC I want to display the Name and Address.City so I did the below
   @Html.Grid(Model).Columns(columns =>
        {                        
              columns.Add(c => c.Name).Titled("Name").Filterable(true);
              columns.Add(c => c.Address.City).Titled("City").Filterable(true);
        }).Sortable(true).WithMultipleFilters()                        
So it works proper even Student Model as Address or not.. But if at least one record exist where Address is null and we click on "City" column to sort the record it throw error.

Image

How to handle it and where to handle I am not getting it. Please help me out.
May 9, 2015 at 6:00 AM
I can't find an issue, but it was already fixed and should be available in one of the pre-release versions. So you would need to update using "Install-Package Grid.Mvc -Pre".
May 11, 2015 at 6:57 AM
@Leya not sure why you can not replicate the same but I'm facing such issue again n again.. so I handled using the below.. its happened when I assigned a foreign key as nullable. I handled using the below inside the .CSHTML at top before calling
@Html.Grid(model)
{....}
I used below
@{
foreach (var model in Model)
    {
        if (model.Address== null)
        {
            model.Address = new Address();
        }
    }
}
..
..
..
..
@Html.Grid(Model).Columns(...)