AjaxCheckBoxList

checkboxlist.png
this helper will create a list of checkboxes, you can call it like this:

<%=Html.AjaxCheckBoxList("Fruits") %>
or
<%=Html.AjaxCheckBoxList("Fruits", controller: "FruitsAjaxCheckBoxList") %>

it requires a controller to get it's data from, if the controller's name is not specified it will search for Prop + "AjaxCheckBoxList" (Fruits + CheckBoxList)
the controller must have an Action GetItems with a keys parameter and it should return a Json made from an IEnumerable<SelectListItem>
    public class FruitsAjaxCheckBoxListController : Controller
    {
        ...
        public ActionResult GetItems(IEnumerable<int> keys)
        {
            keys = keys ?? new List<int>();
            var res = repo.GetAll().Select(o =>
                                        new SelectListItem
                                            {
                                                Text = o.Name,
                                                Value = o.Id.ToString(),
                                                Selected = (keys.Contains(o.Id))
                                            });
            return Json(res);
        }
    }

you can also call it like this:
<%=Html.EditorFor(o => o.Fruits) %>

for this in your ViewModel you will need to do something like this:
[UIHint("AjaxCheckBoxList")]
[AjaxRadioList(Controller = "FruitsAjaxCheckBoxList")]
public IEnumerable<int> TheFruits { get; set; }

[UIHint("AjaxCheckBoxList")]
public IEnumerable<int> Fruits { get; set; }

just like the AjaxDropdown you can bind it with other inputs by specifying the ParentId in the attribute or in the helper's parameters

Last edited May 12, 2011 at 3:02 PM by o, version 2

Comments

o Sep 5, 2011 at 7:01 PM 
with razor is the same just replace the <% with @

mayur_madawar Aug 31, 2011 at 11:36 PM 
How can I use this with Razor View Engine. Can any one please post an sample project.
Thanks