如何用ajax绑定DropDownList的绑定、如何用、ajax、DropDownList

由网友(你必须在、我不想一个人)分享简介:我需要使用Ajax和MVC3从其他的DropDownList(DDL)的结果绑定DDL值I need to bind the ddl values from another dropdownlist(ddl) result using ajax and mvc3 相关阅读:how从控制器得到的结果在AJAX JSO...

我需要使用Ajax和MVC3从其他的DropDownList(DDL)的结果绑定DDL值

I need to bind the ddl values from another dropdownlist(ddl) result using ajax and mvc3

相关阅读:how从控制器得到的结果在AJAX JSON

推荐答案

最好尝试以下方法级联DropDownList的。

Better try the below approach for cascading dropdownlist.

[脚本]

function SetdropDownData(sender, args) {           

        $('#ShipCountry').live('change', function () {
            $.ajax({
                type: 'POST',
                url: 'Home/GetCities',
                data: { Country: $('#ShipCountry').val() },
                dataType: 'json',
                success: function (data) {
                    $('#ShipCity option').remove();
                    $.each(data, function (index, val) {                           
                            var optionTag = $('<option></option>');
                            $(optionTag).val(val.Value).text(val.Text);
                            $('#ShipCity').append(optionTag);

                    });
                }
            });
        });

  }

[控制器]

[Controller]

   public IEnumerable<SelectListItem> Cities(string Country)  // ShipCity is filtered based on the ShipCountry value  
    {
        var Cities = new NorthwindDataContext().Orders.Where(c=>c.ShipCountry == Country).Select(s => s.ShipCity).Distinct().ToList();
        List<SelectListItem> type = new List<SelectListItem>();
        foreach (var city in Cities)
        {
            if (city != null)
            {
                SelectListItem item = new SelectListItem() { Text = city.ToString(), Value = city.ToString() };
                type.Add(item);
            }
        }
        return type;
    }
    public ActionResult GetCities(string Country)  
    {
        return Json(Cities(Country), JsonRequestBehavior.AllowGet);

    }
阅读全文

相关推荐

最新文章