本文介绍了使用JQuery AJAX的MVC级联下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在美国各州都有一个下拉列表.当用户选择州时,将填充县/教区的另一个下拉列表.我正在从存储过程中提取信息以填充县下拉列表.我遇到的问题是,它正在为每个县返回对象Object,但我不知道该怎么做才能纠正此问题.

I have a drop down list of US states. When the user selects the state another drop down list of county/parrish is to be populated. I am pulling the information to populate the county drop down from a stored procedure. The problem I have is that it is returning object Object for each county and I do not know what to do to correct this.

这是控制器中的代码

public ActionResult FillCountyList(string State)
{
    Models.CountyList mod = new CountyList();
    mod.TheState = State;
    List<ListCountiesByState_Result> Counties = mod.ListCounties();
    return Json(Counties, JsonRequestBehavior.AllowGet);
}

我也在控制器中尝试了以下操作,它还返回了一个列表,显示每个县的对象Object

I have also tried to the following in my controller and it also returns a list showing object Object for each county

public JsonResult FillCountyList(string State)
{
    IEnumerable<SelectListItem> Counties = db.ListCountiesByState(State).Select(c => new SelectListItem {
            Value = c.County,
            Text = c.County
    }).ToList();
    return Json(Counties, JsonRequestBehavior.AllowGet);
}

这是视图中的jquery代码

Here is the jquery code in the view

$('#State').change(function () {
    var stateid = $('#State').val();
    $.ajax({
        url: '/Profile/BackgroundData/FillCountyList',
        type: 'GET',
        datatype: 'JSON',
        data: { State: stateid },
        success: function (result) {
            $('#CountyOrParrish').html('');
            $('#CountyOrParrish').append($('<option>Make Selection</option>'));
            $.each(result, function (i, item) {
                $('#CountyOrParrish').append($('<option></option>').text(item));
            });
        }
    });
});

推荐答案

检查您的方法是否正确返回国家/地区.还要更改:

Check That your method returns countries correctly. Also change:

$('<option></option>').text(item));

收件人:

$('<option></option>').val(item.Value).html(item.Text));

这篇关于使用JQuery AJAX的MVC级联下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 16:37