AjaxRadioList
It display a list of radiobuttons, and has mods (Odropodown,
ButtonGroup, .. ) that can change its default behaviour:
@Html.Awe().AjaxRadioList("Category")
@Html.Awe().AjaxRadioListFor(o => o.Meal).Url(Url.Action("Data", "GetMeals"))
Data binding
By default it will look for a controller with the same name as it + "AjaxRadioList" and that controller must have aGetItemsaction, but you can also specify
.Url(str), the same
urlcan be reused by the
AjaxDropdownand
AjaxCheckboxListbecause they all return a collection of
KeyContent. example of AjaxRadioList controller:
public class CategoryAjaxRadioListController : Controller
{
public ActionResult GetItems()
{
return Json(Db.Categories.Select(o => new KeyContent(o.Id, o.Name)));
}
}
Client side data binding
A javascript function can be used to set data instead of the url, it is set using.DataFunc(str)extension, and when specified
.Urlwill be ignored. The js function will receive the same parameters as the controller action, and must also return collection of
KeyContent. example:
<script>Parameters will be in form of name-value pair, and to get them as an object you can use
var meals = @MyUtils.JsonEncode(Db.Meals.Select(o => new KeyContent(o.Id, o.Name)))
function getMealsData(){
return meals;
}
</script>
@Html.Awe().AjaxRadioList("Meals1").DataFunc("getMealsData")
aweUtils.serializeObj:
function getMealsData(params) {
var pobj = aweUtils.serializeObj(params);
Binding to parents / Cascading
The AjaxRadioList (like most of the awesome helpers) can be bound to parents using the.Parent(name, alias="parent", load = true)extension, when the controls loads, the value of the parent will be sent to the server using the alias as parameter name, and when a parent changes value the child will load (if
loadparameter is true).
Parent Category:The action used to get data for the child control receives the value of the parent which can be used to filter the items:
@(Html.Awe().AjaxRadioListFor(o => o.ParentCategory)
.Url(Url.Action("Data", "GetCategories")))
Child Meal:
@(Html.Awe().AjaxRadioListFor(o => o.ChildMeal)
.Parent(o => o.ParentCategory)
.Url(Url.Action("Data", "GetChildMeals")))
public ActionResult GetChildMeals(int? parent)you can bind to as many parents as you need, if you have more than one parent with same alias (parameter name), you will get the value as an array:
{
return Json(Db.Meals.Where(o => o.Category.Id == parent).Select(o => new KeyContent(o.Id, o.Name)));
}
@Html.Awe().AjaxRadioList("MultiChild").Parent("txt1").Parent("txt2").Parent("txt3","par3")
public ActionResult GetItems(int? v, string[] parent, string par3)
{
// string[] parent has value of txt1 and txt2
// par3 has value of txt3
...
}
Parameters
Setting parameters is like setting a parent that never changes its value. example:@(Html.Awe().AjaxRadioListFor(o => o.Meal)In this example the
.Url(Url.Action("Data", "GetMeals")
.Parameter("category", 5))
GetMealsaction will receive
categoryparameter with value = 5.
Comments