ASP.NET - tomar valor seleccionado de un DropDownList mediante json

 
Vista:
sin imagen de perfil
Val: 36
Ha disminuido su posición en 2 puestos en ASP.NET (en relación al último mes)
Gráfica de ASP.NET

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:

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
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder