I am learning MVC 3, MvcContrib grid and when tried to create a view using grid with sort and paging options, I got error. "Could not find a property called 'Customer.CompanyName' on type MvcApplication5.Models.Order" on this line
orders = orders.OrderBy(sort.Column, sort.Direction);
Please let me know whats wrong as the code for Customer table is working correctly may be its just one table and Order table query includes 3 tables?
Controller code is below
public ActionResult Index(GridSortOptions sort, int? page)
{
IEnumerable<Order> orders = db.Orders.Include(o => o.Customer).Include(o => o.Employee).Include(o => o.Shipper);
if (sort.Column != null)
{
orders = orders.OrderBy(sort.Column, sort.Direction);
}
//orders = orders.AsPagination(page ?? 1, 25);
ViewData["sort"] = sort;
return View(orders);
}
View Code is here
@model IEnumerable<MvcApplication5.Models.Order>
@using MvcContrib.UI.Grid;
@using MvcContrib.UI.Pager;
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
@Html.Grid(Model).Sort((GridSortOptions)ViewData["sort"]).Columns(col => {
col.For(o => o.Customer.CompanyName).Named("Customer").Sortable(true).SortColumnName("Customer.CompanyName");
col.For(o => o.Employee.LastName).Named("Employee");
col.For(o => o.OrderDate).Named("Order Date");
col.For(o => o.RequiredDate).Named("Required Date");
col.For(o => o.ShippedDate).Named("Shipped Date");
col.For(o => o.Shipper.CompanyName).Named("Shipper");
col.For(o => o.Freight).Named("Frieght");
col.For(o => o.ShipName).Named("Ship Name");
col.For(o => o.ShipAddress).Named("Ship Address");
col.For(o => o.ShipCountry).Named("Ship Country");
col.For(o => @Html.ActionLink("Edit", "Edit", new { id = o.OrderID }));
col.For(o => @Html.ActionLink("Details", "Details", new { id = o.OrderID }));
col.For(o => @Html.ActionLink("Delete", "Delete", new { id = o.OrderID }));
})
Well I still do not see any answer to my question yet, but while looking into web, I found this link useful somehow and even its not a solution to my problem but still can be used.
ASP.NET MVC Paging/Sorting/Filtering using the MVCContrib Grid and Pager
Here he is using a modified class of product with Category Name column. So instead filling product directly from table, filling productview structure from product and category name and then display its field in the grid. So when user clicks on Category name which is not a direct field of product table, can be sorted and filtered using this way.
But still need to know if we can sort the reference column with this solution as we do for the direct column.
QF