Awesome ASP.net Core and MVC Controls
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 av
parameter which is going to be the keys of the selected itemsaction Search
- gets data for the search result in it's popup, it receives aselected
parameter that represents the selected values, it should return aJson(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)
.Controller<MealsMultiLookupController>()
.ClearButton(Model.ClearButton)
.HighlightChange(Model.HighlightChange)
.DragAndDrop(Model.DragAndDrop)
.Fullscreen(Model.Fullscreen))
Awesome/MultiLookup/MealsMultiLookupController.cs
public class MealsMultiLookupController : Controller
{
public IActionResult 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 IActionResult 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 IActionResult 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))
});
}
}
Comments