Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear sir

I write code where I bind dropdown and take div and button when click on button then 4 control open using jquery so i want to know how to bind dynamic dropdownlist when i append in jquery
i want to bind same dropdown where i take in strongly type dropdownlist in mvc

please help

What I have tried:

 @Html.DropDownListFor(x => x.Source, new SelectList(Model.SourceList, "Code", "Text"), "Select", new { @id = "ddlSourceId", @class = "form-control" })


 <div id="DynamicControl">                                   
                                </div>
                                <div class="col-sm-2">
                                    <div class="form-group">
                                                                      
                                    </div>
                                </div>


  function AddDynamicControl() {
        debugger;
        $("#DynamicControl").append
('<div class="col-sm-3"><div class="form-group">Source<div class="col-sm-5"></div></div></div><div class="col-sm-3"><div class="form-group">Destination <div class="col-sm-9" style="padding-left: 35px"> <div class="col-sm-2"><div class="form-group">STD<div class="col-sm-9"><span class="glyphicon glyphicon-calendar"></span></div></div></div></div></div></div><div class="col-sm-2"><div class="form-group">DateTime<div class="col-sm-9" style="padding-left: 35px"><span class="glyphicon glyphicon-calendar"></span></div></div></div>');
        };
Posted
Updated 20-Nov-22 7:57am
v4
Comments
[no name] 24-Nov-18 11:55am    
You mean to say you have a list of data and you want to bind those data in a dropdown by jquery?

1 solution

you can bind dropdown list dynamically using jquery ajax call to your mvc controller and action.
just make a html feild <selct id="dd">
and call your ajax code on your first dropdown change event.

JavaScript
$('#firstDD').on('change',funtion(){

var id=$(this).val();
$.ajax({
method:'Post',
url:'@Url.Action("Action","Controller")',
data:id,
datatype:'json',
success:function(res){
var op='';
    if(res.length>0){
  <pre>$.each(res, function(k, v) {
    
op +='<option value="'+k.value+'">'+k.text+'</option>';

  });

$('#dd').empty();

$('#dd').append(op);
}
}


});

});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900