MultiLookup




Pick some Meals:
  •  

MultiLookup needs a controller or urls to be specified, by default convention it will look for a controller with the same name as it + "MultiLookupController"

  • action GetItems - used to show the value in the readonly field, it will receive a v parameter which is going to be the keys of the selected items
  • action Search - gets data for the search result in it's popup, it receives a selected parameter that represents the selected values, it should return a Json(AjaxListResult), so it has same features as the AjaxList (table layout, custom item template)
  • action Selected - gets data for the selected items in the popup
MultiLookupDemo/IndexContent.cshtml
@(Html.Awe().MultiLookupFor(o => o.Meals)
.ClearButton(Model.ClearButton)
.HighlightChange(Model.HighlightChange)
.DragAndDrop(Model.DragAndDrop)
.Fullscreen(Model.Fullscreen))
Awesome/MultiLookup/MealsMultiLookupController.cs
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))
});
}
}