Lookup Custom Search

Meal:
 

Custom search is achieved by setting .CustomSearch(true) and adding a SearchForm action in the Lookup controller which will render the custom SearchForm view


LookupDemo/CustomSearch.cshtml
@Html.Awe().Lookup("MealCustomSearch").CustomSearch()
Awesome/Lookup/MealCustomSearchLookupController.cs
using System.Linq;
using Microsoft.AspNetCore.Mvc;

using AweCoreDemo.Models;
using AweCoreDemo.Utils;
using Omu.AwesomeMvc;
using System.Threading.Tasks;
using AweCoreDemo.Data;
using AweCoreDemo.Utils.Awesome;

namespace AweCoreDemo.Controllers.Awesome.Lookup
{
public class MealCustomSearchLookupController : Controller
{
private readonly MyContext mcx = new MyContext();// mock EF Db context

public MealCustomSearchLookupController()
{

}

public IActionResult SearchForm()
{
return PartialView();
}

public async Task<IActionResult> GetItem(int? v)
{
Check.NotNull(v, "v");
var o = await mcx.FindAsync<Meal>(v) ?? new Meal();

return Json(new KeyContent(o.Id, o.Name));
}

public async Task<IActionResult> Search(int[] categories, int page, string search = "")
{
categories = categories ?? new int[] { };

var query = mcx.Meals.Filter(search, f => f.Name);
if (categories.Any())
{
query = query.Where(f => categories.Contains(f.Category.Id));
}

var res = await EFAweUtil.BuildListAsync(query, page, keyContentFunc: o => new KeyContent(o.Id, o.Name));

return Json(res);
}
}
}
MealCustomSearchLookup/SearchForm.cshtml
@using AweCoreDemo.Models
@{ Layout = null; }
<div class="srcfitem">
@Html.Awe().TextBox("Search").CssClass("awe-searchtxt").Placeholder("search")
</div>
<div class="srcfitem">
@(Html.Awe().DropdownList(new DropdownListOpt
{
Name = "Categories",
Url = Url.Action("GetCategories", "Data"),
Value = DemoCache.Categories.First().Id,
Prefix = "MealLookup"
}))
</div>



Comments