Lookup
It can be declared using theLookupor
LookupForhelper 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
.Controllerextension example:
@Html.Awe().Lookup("Category")- will look for
CategoryLookupController.GetItemand
Searchactions
@Html.Awe().Lookup("Category1").Controller("CategoryLookup")
@Html.awe().Lookup("Meal1").Controller("Foo")- looks for
FooController.GetItemand
Searchactions 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
AjaxListhelper. 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 namedsearch, that's why we have the
string searchparameter in the
Searchaction, 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
SearchFormaction in the lookup controller:
public class MealCustomSearchLookupController : Controllerexample of
{
public ActionResult SearchForm()
{
return PartialView();
}
...
SearchForm.cshtml:
@Html.Awe().TextBox("Search").CssClass("awe-searchtxt").Placeholder("search")
@(Html.Awe().Odropdown("Categories").Url(Url.Action("GetCategories", "Data")))
Comments