如何使用ajax从ASP.NET MVC中的数据库填充dropdownlist

问题描述:

我的mvc应用程序中有一个下拉列表,当页面加载时加载,但我不想这样做而是我想点击下拉列表并加载整个数据是否可以吗?



我尝试过:



< script>

$(#countrySelect )。change($(document).ready(function(){

debugger;

$ .get('Home / countrySelectList',function(data){

console.log(数据)

$ .each(数据,函数(索引,值){

$('< option>' ).val(value)。text(value).appendTo(#countrySelect);

});

});

});

< / script>

i have a dropdownlist in my mvc application which is loaded when the page load but i dont wantto do that instead i want to click the dropdown and load entire data is it possoble?

What I have tried:

<script>
$("#countrySelect").change($(document).ready(function () {
debugger;
$.get('Home/countrySelectList', function(data) {
console.log(data)
$.each(data, function(index, value) {
$('<option>').val(value).text(value).appendTo("#countrySelect");
});
});
}));
</script>

(#countrySelect)。change(
("#countrySelect").change(


(document).ready(function(){

debugger;
(document).ready(function () {
debugger;


.get('H ome / countrySelectList',function(data){

console.log(data)
.get('Home/countrySelectList', function(data) {
console.log(data)