Awesome ASP.net Core and MVC Controls

Lookup with 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
public class MealCustomSearchLookupController : Controller
{
public ActionResult SearchForm()
{
return PartialView();
}

public ActionResult GetItem(int? v)
{
Check.NotNull(v, "v");
var o = Db.Meals.SingleOrDefault(f => f.Id == v) ?? new Meal();

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

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

var list = Db.Meals.Where(f => f.Name.ToLower().Contains(search) //give list of meals where name contains search
&& (!categories.Any() || f.Category != null && categories.Contains(f.Category.Id)));// if categories specified, filter by them
return Json(new AjaxListResult
{
Items = list.Skip((page - 1) * PageSize).Take(PageSize).Select(o => new KeyContent(o.Id, o.Name)),
More = list.Count() > page * PageSize
});
}
}
MealCustomSearchLookup/SearchForm.cshtml
@using AwesomeMvcDemo.Models
@{ Layout = null; }
<div class="srcfitem">
@Html.Awe().TextBox("Search").CssClass("awe-searchtxt").Placeholder("search")
</div>
<div class="srcfitem">
@(Html.Awe().Odropdown("Categories").Url(Url.Action("GetCategories", "Data")).Value(Db.Categories.First().Id).Prefix("MealLookup"))
</div>



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.