Return a value from a DropDownList

0

How can I get the value of a DropDownList in the controller, when I select an option I want to fill another DropDownList with a cascaded script

On the Controller I filled DropDownList with ViewBag.Id_Business

public JsonResult GetPasillos(int idEmpresa)
  {
    var listaBodegas = db.Tabla_Bodega.Where(b => b.id_empresa == idEmpresa);
    return Json(listaBodegas);
  }

public ActionResult Create()
  {
    ViewBag.Id_Empresa = new SelectList(db.Tabla_Empresa, "id_empresa", "nombre_empresa");
    ViewBag.Id_bodega = new SelectList(db.Tabla_Bodega, "Id_bodega", "Nombre_bodega");
    return View();
   }

And in the Vista I receive it like this. Then with the script I want to make the Cascade

@using (Html.BeginForm())
{
  <div class="form-group">
    <div class="col-md-10">
      @Html.DropDownList("Id_Empresa", null, htmlAttributes: new { @class = "form-control" })
    </div>
  </div>
}
@section Scripts {
  @Scripts.Render("~/bundles/jqueryval")
  <script type="text/javascript">
    $(document).ready(function () {
      $("#Id_Empresa").change(function () {
        $("#Id_bodega").empty();
        $.ajax({
          type: 'POST',
          url: '@Url.Action("GetPasillos")',
          dataType: 'json',
          data: { empresaId: $("#Id_Empresa").val() },
          success: function (bodegas) {
            $.each(bodegas, function (i, bodega) {
              $("#Id_bodega").append('<option value="'
               + bodega.Id_bodega + '">'
               + bodega.Nombre_bodega + '</option>');
            });
          },
          error: function (ex) {
            alert('Falle en Recuperar las Bodegas.' + ex);
          }
        });
          return false;
      })
      });
  </script>
}

Thank you in advance for your help.

    
asked by Miguel 12.06.2018 в 06:49
source

0 answers