Awesome/Grid/LunchGridController.cs
using AwesomeWebFormsDemo.Data;
using AwesomeWebFormsDemo.Models;
using AwesomeWebFormsDemo.Utils.Awesome;
using System.Web.Mvc;
using Omu.AwesomeWebForms;
using System.Linq;
using System.Threading.Tasks;
namespace AwesomeWebFormsDemo.Controllers.Awesome.Grid
{
public class LunchGridController : Controller
{
private readonly MyContext mcx = new MyContext();// mock EF Db context
public LunchGridController()
{
}
public ActionResult GetItems(GridParams g, int? country, string person, string food)
{
// get Lunch[] sample data (see other demos for EF demo)
var query = getLunchesArray().AsQueryable();
// filtering
if (person != null) query = query.Where(o => o.Person.ToLower().Contains(person.ToLower()));
if (food != null) query = query.Where(o => o.Food.Name.ToLower().Contains(food.ToLower()));
if (country.HasValue) query = query.Where(o => o.Country.Id == country);
// data can be from any source as long here you pass an IQueryable<T>
var gmb = new GridModelBuilder<Lunch>(query.AsQueryable(), g);
// define grid key, used to identify rows
gmb.KeyProp = o => o.Id;
// grid row model
// columns bound to Prop1.Prop2 will automatically display Prop1Prop2,
// or you could specify in the view Column.Prop ="Prop1Prop2", or ClientFormat = ".(Prop1Prop2)"
gmb.Map = o => new
{
o.Id,
o.Person,
FoodName = o.Food.Name,
FoodPic = o.Food.Pic,
o.Location,
o.Price,
Date = o.Date.ToShortDateString(),
CountryName = o.Country.Name,
ChefName = o.Chef.FullName
};
return Json(gmb.Build());
}
private Lunch[] getLunchesArray()
{
return mcx.Lunches
.Include(o => o.Country)
.Include(o => o.Food)
.Include(o => o.Chef)
.ToArray();
}
public async Task<ActionResult> GetItemsEFExample(GridParams g, int? country, string person, string food)
{
var query = mcx.Lunches
.Include(o => o.Country)
.Include(o => o.Food)
.Include(o => o.Chef)
.AsQueryable();
// Filter - custom extension in demo code
// equivalent to .Where(o => o.Food.Name.Contains(food) || ...)
query = query
.Filter(food, o => o.Food.Name)
.Filter(person, o => o.Person);
if (country.HasValue) query = query.Where(o => o.Country.Id == country);
var gmb = new GridModelBuilder<Lunch>(query, g);
gmb.KeyProp = o => o.Id;
gmb.Map = o => new
{
o.Id,
o.Person,
FoodName = o.Food.Name,
FoodPic = o.Food.Pic,
o.Location,
o.Price,
Date = o.Date.ToShortDateString(),
CountryName = o.Country.Name,
ChefName = o.Chef.FullName
};
return Json(await gmb.EFBuildAsync());
// you can use .Build() for non EF datasource (like a List<Lunch> query)
// EFBuildAsync - custom extension for EF, you can copy it from demo source code
}
}
}