Create Select in cascade

0

I have two select controls that I created them like this:

 @Html.Action("BuscarArticuloLinea", "ArticuloLinea", New With {.IdArticuloLinea = 0})
 @Html.Action("BuscarArticuloSubLinea", "ArticuloSubLinea", New With {.IdArticuloSubLinea = 0, .IdArticuloLinea = 0})

In the case of the first

  

"SearchArticleLine"

calls a controller that invokes a partial view in which I create the select ... the controller is in charge of filling that select. "In the case of the second select it has to be filled when choosing an item from the select. first select ... that's what I can not do.When loading the page the two combos are created and only the first is filled as it should be.What is the way to fill the second select when selecting an item from the first one? ... I await your answers ... thanks

    
asked by edusito87 15.01.2018 в 18:49
source

1 answer

0

This method is in the Controller

public JsonResult BuscarArticulos(string id)
    {
        var query= db.Articulos.Where(m => m.IdPadre.Equals(Convert.ToInt32(id))).ToList();
        return Json(new SelectList(query, "Id", "Nombre"));
    }

View

<script type="text/javascript">
$(document).ready(function () {
    $("#BuscarArticuloLinea").change(function () {
        $("#BuscarArticuloSubLinea").empty();
        $.ajax({
            type: 'POST',
            url: '@Url.Action("BuscarArticulos")',
            dataType: 'json',
            data: { id: $("#BuscarArticuloLinea").val() },
            success: function (articulos) {
                $.each(articulos, function (i, data) {
                    $("#BuscarArticuloSubLinea").append('<option value="' + data.Value + '">' + data.Text + '</option>');
                });
            },
            error: function (ex) {
                alert('Error.' + ex);
            }
        });
        return false;
    })
});

@Html.DropDownListFor(model => model.BuscarArticuloLinea, ViewBag.BuscarArticuloLinea as SelectList, "Seleccione Articulo en Linea", htmlAttributes: new { @class = "form-control" })
@Html.DropDownList("BuscarArticuloSubLinea", new SelectList(string.Empty, "Value", "Text"), "Seleccione Articulo en SubLinea", htmlAttributes: new { @class = "form-control" })
    
answered by 16.01.2018 в 09:05