Cannot get AjaxDropdown to work for editing

Feb 19, 2011 at 10:14 AM

How do you get the AjaxDropdown to select the currently selected item in the list? I use Html.AjaxDropdown("inventory_code") to display the entire list of inventory codes. It displays correctly and when I postback it correctly sends the id-value of the selected item. 

I cannot get it to highlight the current selection in the dropdown when I want to do an edit. I know there is a key that must be passed to the GetItems() function but I cannot figure out how to get it to pass the key. Do you have to call the AjaxDropdown with different

parameters, and if so, what are the required parameters?

Coordinator
Feb 19, 2011 at 11:08 AM

this helper works as any other standard mvc helper,

in your case if your view is strongly typed to a viewmodel and in that viewmodel you have a property "inventory_code", it will get the value from there

you can also set the value explicitly by doing Html.AjaxDropdown(...., value = 123)

Feb 22, 2011 at 8:39 AM

Thank you for the reply. The issue was that I needed to provide a prefix to ensure unique IDs.

Mar 22, 2011 at 9:19 AM

Hi all,

When I edit data I can't get parent

I have 2 AjaxDropdown:

1) @Html.AjaxDropdown("Organization", controller: "Organization", htmlAttributes: new { @class = "original" })

Controller :

 public ActionResult GetItems(string key)
        {
           List<OrganizationView> organizationViewList = _organizationService.FindAll();
            List<SelectListItem> list;
            list = new List<SelectListItem>
                           {
                               new SelectListItem
                                   {Text =organizationViewList[2].CustOrgName, Value = organizationViewList[2].CustOrgName, Selected= true}
                           };

            return Json(list);
        }

 

2)  @Html.AjaxDropdown("Division", controller: "Division", parentId: "Organization", htmlAttributes: new { @class = "original" })

Controller:

 public ActionResult GetItems(string key, string parent)
        {
            List<Division> divisionViewList = _divisionService.GetAllDivisionForDropDownList(parent);
            List<SelectListItem> list;
            if (divisionViewList.Count == 1)
            {
                list = new List<SelectListItem>
                           {
                               new SelectListItem
                                   {Text = divisionViewList[0].CustDivName, Value = divisionViewList[0].CustDivId,Selected =true}
                           };
            }
            else
            {
                list = new List<SelectListItem> { new SelectListItem { Text = CommonMessages.SELECT_ONE, Value = CommonMessages.SELECT_ONE_VALUE,Selected=true }};
                list.AddRange(
                    divisionViewList.Select(o => new SelectListItem
                                                     {
                                                         Text = o.CustDivName,
                                                         Value = o.CustDivId.ToString(),
                                                         Selected = o.CustDivId == key
                                                     }));
            }
            list.Add(new SelectListItem {Text = "Create a New Division", Value = CommonMessages.CREATE_NEW_DIVISION });

            return Json(list);
        }

===> I can't get parent there. It's can't binding data into second AjaxDropdown

Please help me

Thanks, :)

Mar 23, 2011 at 2:24 AM

Please help me to fix this problem

Thanks :)

Coordinator
Mar 23, 2011 at 7:05 AM

look in firebug's console what data is being posted when you change the value of the dropdown it should be the key and the parent