This is a migrated thread and some comments may be shown as answers.

[Solved] Dynamic DropDownList

1 Answer 145 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Taras
Top achievements
Rank 1
Taras asked on 31 Aug 2011, 07:36 AM
Hi,
Sorry for my English.
I need dynamic dropdownlist. I have 2 dropdownlists, and i want that after choose data in first dropdown, second dropdown filling data with parameter from first dropdown.
I have View
@(Html.Telerik().ComboBox()
    .Name("AjaxComboBox")
    .AutoFill(true)
    .DataBinding(binding => binding.Ajax().Select("_AjaxLoading", "Home"))
    .Filterable(filtering =>
    {
        filtering.FilterMode(AutoCompleteFilterMode.StartsWith);
    })
    .HighlightFirstMatch(true)
)
 
@(Html.Telerik().DropDownList()
    .Name("AjaxDropDownList")
    .DataBinding(binding => binding.Ajax().Select("_AjaxLoading2", "Home", new { id = ViewData["AjaxComboBox"] }))
)

and Controller
[HttpPost]
public ActionResult _AjaxLoading(string text)
{
    Thread.Sleep(1000);
    using (var nw = new InetShopDBEntities())
    {
        var producttypes = nw.TypeOf.AsQueryable();
        return new JsonResult { Data = new SelectList(producttypes.ToList(), "TypeOf_ID", "Name") };
    }
}
 
[HttpPost]
public ActionResult _AjaxLoading2(string text, int id)
{
    Thread.Sleep(1000);
    using (var nw = new InetShopDBEntities())
    {
        var products = nw.Product.AsQueryable();
        products = products.Where(p => p.TypeOf == id);
        return new JsonResult { Data = new SelectList(products.ToList(), "Product_ID", "Name") };
    }
}

But it's not work. How I can do it?

1 Answer, 1 is accepted

Sort by
0
Nohinn
Top achievements
Rank 1
answered on 31 Aug 2011, 04:12 PM
If your second combobox should be populated based on the first combobox selection your best approach is this:
1) In the view where you have the first combobox add a div at the end with an id, for example:
<div id="divload"/>

2) Put the code for your second combobox into another View (this implies making an action in your controller with the param you will be sending from the first combobox)
public ActionResult PopulateCombobox2(string myparam) {
    ViewData["myparam"] = myparam;
    Return View();
}

3) In your second comboboxview, in this example PopulateCombobox2 make sure to use that param (it will be the selected option from the first combobox) in the Select binding.
4) With javascript:
$("#AjaxComboBox-input").change(function(){
     var value = $(this).val();
     $("#divload").load('UrlToYourActionThatShowsTheSecondCombo',
                                       {
                                              myparam: value
                                       });
});

That "divload" can be whatever you set as the id of the div from point number 1.
Make sure you point to the right url as the first param of the load method in javascript.
And the params between brackets:
{
    myparam: value
});

Should match the name of the params you have in your action method, if you have more than one param you should separate them with a comma
{
    param1: value1,
    param2: value2,
    param3: value3
});
Tags
ComboBox
Asked by
Taras
Top achievements
Rank 1
Answers by
Nohinn
Top achievements
Rank 1
Share this question
or