MultiLookup
Declaration:<% Meal1.MultiLookup().Controller("MealsMultiLookup"); %>
<awe:Ocon runat="server" ID="Meal1" />
Data binding
It needs a controller to get data from, the controller must have 3 actions:GetItems- display the selected values
Search- popup search
Selected- popup selected items Instead of specifying a controller 3 urls can be specified using extensions
.GetItemsUrl(str),
.SearchUrl(str),
.SelectedUrl(str). Both the
Searchand
Selectedactions will get the
selectedparameter which in case of
Searchaction will be used to exclude the selected items from the search result, and in case of
Selectedaction will be used to get the selected items, example:
public class MealsMultiLookupController : Controller
{
public ActionResult GetItems(int[] v)
{
var items = new List<Meal>();
if (v != null)
{
items.AddRange(v.Select(Db.Get<Meal>));
}
return Json(items.Select(meal => new KeyContent(meal.Id, meal.Name)));
}
public ActionResult Search(string search, int[] selected, int page)
{
const int pageSize = 10;
selected = selected ?? new int[] { };
search = (search ?? "").ToLower().Trim();
var items = Db.Meals.Where(o => o.Name.ToLower().Contains(search) && !selected.Contains(o.Id));
return Json(new AjaxListResult
{
Items = items.Skip((page - 1) * pageSize).Take(pageSize).Select(o => new KeyContent(o.Id, o.Name)),
More = items.Count() > page * pageSize
});
}
public ActionResult Selected(int[] selected)
{
var items = new List<Meal>();
if (selected != null)
{
items.AddRange(selected.Select(Db.Get<Meal>));
}
return Json(new AjaxListResult
{
Items = items.Select(o => new KeyContent(o.Id, o.Name))
});
}
}
Applies to all version