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)
.Controller<MealsMultiLookupController>()
.ClearButton(Model.ClearButton)
.HighlightChange(Model.HighlightChange)
.DragAndDrop(Model.DragAndDrop)
.Fullscreen(Model.Fullscreen))
Awesome/MultiLookup/MealsMultiLookupController.cs
using AweCoreDemo.Data;
using AweCoreDemo.Models;
using AweCoreDemo.Utils.Awesome;
using Microsoft.AspNetCore.Mvc;
using Omu.AwesomeMvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace AweCoreDemo.Controllers.Awesome.MultiLookup
{
public class MealsMultiLookupController : Controller
{
private readonly MyContext mcx = new MyContext();// mock EF Db context
public MealsMultiLookupController()
{
}
public async Task<IActionResult> GetItems(int[] v)
{
var items = new List<Meal>();
if (v != null)
{
items = await mcx.Meals
.Where(o => v.Contains(o.Id)).ToListAsync();
}
return Json(items.Select(meal => new KeyContent(meal.Id, meal.Name)));
}
public async Task<IActionResult> Search(int[] selected, int page, string search = "")
{
selected = selected ?? new int[] { };
var query = mcx.Meals.Where(o => o.Name.Contains(search) && !selected.Contains(o.Id));
var res = await EFAweUtil.BuildListAsync(query, page, keyContentFunc: o => new KeyContent(o.Id, o.Name ));
return Json(res);
}
public async Task<IActionResult> Selected(int[] selected)
{
var items = new List<Meal>();
if (selected != null)
{
items = await mcx.Meals
.Where(o => selected.Contains(o.Id)).ToListAsync();
}
return Json(new AjaxListResult
{
Items = items.Select(o => new KeyContent(o.Id, o.Name))
});
}
}
}