tomar valor seleccionado de un DropDownList mediante json
Publicado por fernando (26 intervenciones) el 03/11/2017 16:12:05
Hola el problema que tengo es que cuando entra a este metodo me llega siempre en null:
DONDE CARGO EL DropDownList:
LO MUESTRO ACA:
Y ESTE ES EL SCRIPT DONDE HACE LA MAGIA:
Desde ya gracias!
Saludos!
1
2
3
4
5
6
7
8
9
10
11
public JsonResult State_Bind(string nombreEvento)
{
DataSet ds = bc.getServicioDelEvento(nombreEvento);
List<SelectListItem> comentarios = new List<SelectListItem>();
foreach (DataRow item in ds.Tables[0].Rows)
{
comentarios.Add(new SelectListItem { Text = item["Texto"].ToString(), Value = item["id"].ToString() });
}
return Json(comentarios, JsonRequestBehavior.AllowGet);
}
DONDE CARGO EL DropDownList:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[HttpGet]
public ActionResult IngresarEvento()
{
Libro_Bind();
return View();
}
public void Libro_Bind()
{
DataSet ds = bc.getEventos();
List<SelectListItem> libroLista = new List<SelectListItem>();
foreach (DataRow item in ds.Tables[0].Rows)
{
libroLista.Add(new SelectListItem { Text = item["nombreEvento"].ToString(), Value = item["nombreEvento"].ToString() });
}
ViewBag.Evento = libroLista;
}
LO MUESTRO ACA:
1
@Html.DropDownList("Evento", null, "Seleccione uno")
Y ESTE ES EL SCRIPT DONDE HACE LA MAGIA:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@section Scripts {
<script type ="text/javascript">
$(document).ready(function () {
$("#Evento").change(function () {
$("#Proveedor").empty();
$.ajax({
type: 'POST', url: '@Url.Action("State_Bind")', dataType: 'json', data: { nombreEvento: $("#nombreEvento").val() },
success: function (municipalities) {
$.each(municipalities, function (i, municipality) {
$("#Proveedor").append('<option value="' + municipality.MunicipalityId + '">' + municipality.Name + '</option>');
});
},
error: function (ex) {
alert('Failed to retrieve municipalities.' + ex);
}
}); return false;
})
});
</script>
}
Desde ya gracias!
Saludos!
Valora esta pregunta


0