Asp .NET MVC下拉列表级联检索值

时间:2013-01-28 13:43:10

标签: jquery asp.net-mvc json asp.net-mvc-3 asp.net-mvc-4

我以我的形式使用Asp.Net MVC4我在级联中有下拉列表,当我在另一个DropDownList中选择“Distrito”时加载“Sucursal”的值,这正常工作我可以插入记录我的问题是当我需要时搜索项目以检索未设置正确值的数据。 这是控制器中的代码:

public JsonResult Buscar(string id){
        string Mensaje = "";
        Models.cSinDenuncias oDenuncia = new Models.cSinDenuncias();
        oDenuncia.sd_iddenuncia = id;
        var denuncia = Servicio.RecuperaDenuncia<Models.cSinDenuncias>(ref Mensaje, oDenuncia.getPk(), oDenuncia);
        string HoraDenuncia = denuncia.sd_horadenuncia.ToString();
        return Json(new {objDenuncia = denuncia, hDenuncia = HoraDenuncia});
    }
在objDenuncia中的

我有值,例如值objDenuncia.sd_pardistrito =“TJA”objDenuncia.sd_sucursal =“YCB”

当我发现这是我在视图中的代码时:

function Buscar() {
        var Iddenuncia = $('#sd_iddenuncia').val();
        $.ajax({
            url: '@Url.Action("Buscar", "raDenuncia")',
            type: 'POST',
            data: { Id: Iddenuncia },
            dataType: 'json',
            success: function (data) {                    
                $('#sd_iddenuncia').val(data.objDenuncia.sd_iddenuncia);
                $('#ddlParDistrito').val(data.objDenuncia.sd_pardistrito);
                $('#ddlParDistrito').trigger('change');
                $('#ddlSucursal').trigger('change');
                $('#ddlSucursal').val();                    
                $('#ddlSucursal').val(data.objDenuncia.sd_sucursal);                    
                $("select#ddlSucursal").val(data.objDenuncia.sd_sucursal);                  
            }
        });
    }

下拉列表中的值ddlParDistrito是“TJA”文本是“TARIJA”,它很好地显示了组件中的文本执行$('#ddlParDistrito')。触发器('更改');这个加载数据为下拉列表“Sucursal”但没有设置当前值,这个showme“ - SUCURSAL - ”这里的当前值应该是“YCB”,文本“YACUIBA”请帮我解决这个问题。

此致 里卡多

在我看来,我有这个下拉列表,对于“SUCURSAL”我使用新的SelectList(Enumerable.Empty()

 <td class="name"><label>Distrito:</label></td>
 <td class="name">@Html.DropDownListFor(u => u.sd_pardistrito, new SelectList(ViewBag.Distrito as System.Collections.IEnumerable, "cp_idparametro", "cp_descparametro"), "-- DISTRITO --", new { id = "ddlParDistrito", style = "width:125px;" })</td>
 <td class="name"><label>Sucursal:</label></td>
 <td class="name">@Html.DropDownListFor(u => u.sd_sucursal, new SelectList(Enumerable.Empty<SelectListItem>(), "cp_idparametro", "cp_descparametro"), "-- SUCURSAL --", new { id = "ddlSucursal", style = "width:125px;" })</td>

在我的控制器中,我使用此方法在“DISTRITO”中更改选项时加载下拉列表

[AcceptVerbs(HttpVerbs.Get)]
    public JsonResult LoadSucusalByDistrito(string id)
    {
        var sucursalList = this.GetSucursal(id);
        var sucursalData = sucursalList.Select(m => new SelectListItem()
        {
            Text = m.cp_descparametro,
            Value = m.cp_idparametro,
        });
        return Json(sucursalData, JsonRequestBehavior.AllowGet);
    }

在我看来,当更改选项ddlDistrito加载ddlSucursal

$("#ddlParDistrito").change(function () {
            var idDistrito = $(this).val();
            $.getJSON("/raDenuncia/LoadSucusalByDistrito", { id: idDistrito },
                function (distritoData) {
                    var select = $("#ddlSucursal");
                    select.empty();
                    select.append($('<option/>', {
                        value: 0,
                        text: "-- SUCURSAL --"
                    }));
                    $.each(distritoData, function (index, itemData) {
                        select.append($('<option/>', {
                            value: itemData.Value,
                            text: itemData.Text
                        }));
                    });
                });
        });

1 个答案:

答案 0 :(得分:0)

我在问题下面的评论回复中没有清楚地理解你的陈述。

我将尝试解释我认为可能存在的问题,如果我处理某事,你可以告诉我,当执行语句$('#ddlSucursal').val(data.objDenuncia.sd_sucursal);时,我认为下拉列表ddlSucursal没有数据。有没有办法让你在$("#ddlParDistrito").change...函数中移动该语句,以便在完成后执行。

如果您可以尝试以下操作,那么效果如下:

var currentSucursal = null;
function Buscar() {
    var Iddenuncia = $('#sd_iddenuncia').val();
    $.ajax({
        url: '@Url.Action("Buscar", "raDenuncia")',
        type: 'POST',
        data: { Id: Iddenuncia },
        dataType: 'json',
        success: function (data) {                    
            $('#sd_iddenuncia').val(data.objDenuncia.sd_iddenuncia);
            $('#ddlParDistrito').val(data.objDenuncia.sd_pardistrito);

            // Set the global variable to the currently given sucursal value
            currentSucursal = data.objDenuncia.sd_sucursal;

            // Call the change event after the global values is set
            $('#ddlParDistrito').trigger('change');
            // $('#ddlSucursal').trigger('change');
        }
    });
}

然后

$("#ddlParDistrito").change(function () {
    var idDistrito = $(this).val();
    $.getJSON("/raDenuncia/LoadSucusalByDistrito", { id: idDistrito },
        function (distritoData) {
            var select = $("#ddlSucursal");
            select.empty();
            select.append($('<option/>', {
                value: 0,
                text: "-- SUCURSAL --"
            }));
            $.each(distritoData, function (index, itemData) {
                select.append($('<option/>', {
                    value: itemData.Value,
                    text: itemData.Text
                }));
            });

            // Check if the global variable has value
            if(null!==currentSucursal) {
                $('#ddlSucursal').val(currentSucursal);
            }
        });
});

我希望我的内联评论是自我解释的。