Lookup

It can be declared using the
Lookup
or
LookupFor
helper like this:
@Html.Awe().LookupFor(o => o.Meal) 
@Html.Awe().Lookup("Category")

Data binding

By default the will get its data from the controller with the same name as it +
LookupController
, but you can specify the controller using
.Controller
extension example:
@Html.Awe().Lookup("Category")
- will look for
CategoryLookupController.GetItem
and
Search
actions
@Html.Awe().Lookup("Category1").Controller("CategoryLookup")
@Html.awe().Lookup("Meal1").Controller("Foo")
- looks for
FooController.GetItem
and
Search
actions the lookup controller must have 2 actions:
GetItem
- used to display the selected value.
Search
- used when the user opens the Lookup's popup and searches for items, it must return
Json(AjaxListResult)
and pagination, searching is done the same as for the
AjaxList
helper. Instead of specifying a controller 2 urls can be specified using these extensions .GetItemUrl(str), .SearchUrl(str). example of a lookup controller:
public class MealLookupController : Controller
{
    public ActionResult GetItem(int? 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 page)
    {
        search = (search ?? "").ToLower().Trim();
        var list = Db.Meals.Where(f => f.Name.ToLower().Contains(search));
        return Json(new AjaxListResult
        {
            Items = list.Skip((page-1) * 7).Take(7).Select(o => new KeyContent(o.Id, o.Name)),
            More = list.Count() > page * 7
        });
    }
}

Custom search

By default the lookup popup will contain a textbox named
search
, that's why we have the
string search
parameter in the
Search
action, but this textbox can be replaced with our own custom partial view. For this first specify that custom search is used:
@Html.Awe().Lookup("MealCustomSearch").CustomSearch()
and add the
SearchForm
action in the lookup controller:
public class MealCustomSearchLookupController : Controller
{
    public ActionResult SearchForm()
    {
        return PartialView();
    }
...
example of
SearchForm.cshtml
:
@Html.Awe().TextBox("Search").CssClass("awe-searchtxt").Placeholder("search")
@(Html.Awe().Odropdown("Categories").Url(Url.Action("GetCategories", "Data")))



Comments
By accessing this site, you agree to store cookies on your device and disclose information in accordance with our cookie policy and privacy policy .