Search code examples
c#asp.net-mvcteleriktelerik-mvctelerik-combobox

ASP.NET Telerik combobox is not being updated with new values


I have an ASP.NET MVC application which uses Telerik controls. I am new in Telerik controls.

In my asp.net view (.cshtml) I have below Telerik combobox defined:

<div class="inline-form-field">
    <label>@Ubicaciones.lblTipoVia.ToUpper()</label>
    @(Html.Telerik().DropDownListFor(t => t.tipoViaId)                              
        .BindTo(new SelectList(@ViewBag.tiposVia, "tipoViaId", "descripcion"))
    .HtmlAttributes(new { style = "width: 190px;" })

    )
</div>

Later in the same view, when a condition is satisfied I call below below javascript function in order to update the combox with new values coming from a function in ASP.NET MVC controller:

function cargarTiposVia(){        
    var comboTiposVia = $('#tipoViaId').data('tDropDownList').value();
    var actionUrl = '@Url.Action("GetTiposVia", "Ubicaciones")?municipioId=' + $('#codigoMunicipio').val() + '&localidadId=' + $('#codigoLocalidad').val() ;
    $.ajax({
        url: actionUrl,
        async:false,
        type: "POST",
        traditional: true,
        success: function (data) {
            if (data)
            {       
                comboTiposVia.dataBind(data);
            }
        }
    });
}

Below the function GetTiposVia in the ASP.NET MVC Controller:

    public JsonResult GetTiposVia(string municipioId, string localidadId)
    {
        CommonManager commonManager = new CommonManager(currentUserSociedadId);
        List<My.DTOs.TipoViaDTO> tiposVia = commonManager.getTiposViaByMunicipio(municipioId);
        //ViewBag.tiposVia = tiposVia;
        return Json(new SelectList(tiposVia, "tipoViaId", "descripcion"), JsonRequestBehavior.AllowGet);
    }

Ajax result is success (I have checked by putting an alert), so comboTiposVia.dataBind(data) is executed but combox is not loaded with the new values. I do not understand what is happening...


Solution

  • I have solved. The problem was I was taken the current item selected value in the combobox and not the combobox instance.

    Changing:

    var comboTiposVia = $('#tipoViaId').data('tDropDownList').value();
    

    by

    var comboTiposVia = $('#tipoViaId').data('tDropDownList');
    

    is working.