Awesome ASP.net Core and MVC Controls

MultiLookup Custom Search

Meals:
  •  

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

MultiLookupDemo/CustomSearch.cshtml
@(Html.Awe().MultiLookup("MealsCustomSearch")
.CustomSearch())
Awesome/MultiLookup/MealsCustomSearchMultiLookupController.cs
public class MealsCustomSearchMultiLookupController : Controller
{
//when CustomSearch(true) this action will be rendered instead of the default search
public ActionResult SearchForm()
{
return PartialView();
}

public ActionResult GetItems(IEnumerable<int> v)
{
return Json(Db.Meals.Where(o => v != null && v.Contains(o.Id))
.Select(f => new KeyContent(f.Id, f.Name)));
}

public ActionResult Search(string search, int[] selected, int[] categories, int page)
{
const int pageSize = 10;
selected = selected ?? new int[] { };
search = (search ?? "").ToLower().Trim();
categories = categories ?? new int[] { };

var items = Db.Meals.Where(o => o.Name.ToLower().Contains(search)
&& !selected.Contains(o.Id)
&& (categories.Contains(o.Category.Id) || categories.Count() == 0));

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)
{
selected = selected ?? new int[] { };
return Json(new AjaxListResult
{
Items = Db.Meals.Where(o => selected.Contains(o.Id))
.Select(o => new KeyContent(o.Id, o.Name))
});
}
}
MealsCustomSearchMultiLookup/SearchForm.cshtml
@using AwesomeMvcDemo.Models
@{ Layout = null; }
@Html.Awe().TextBox("Search").CssClass("awe-searchtxt").Placeholder("search").HtmlAttributes(new{ style="min-width:150px;"})
@(Html.Awe().AjaxRadioList("Categories")
.Url(Url.Action("GetCategories", "Data"))
.Value(Db.Categories.First().Id)
.Prefix("MealLookup")
.Odropdown())



Comments

We use cookies to improve your online experience. By accessing this site, you agree to the use of cookies and other technologies to process your personal data (e.g. IP address) to enhance and personalise your experience across our websites. Additionally, third-party companies, may store cookies on your device and use similar technologies to collect and use certain information. For more information, refer to our privacy policy and cookie policy.