i´m new developing on asp.net mvc, and even newer using jquery, so this is my problem: i have a few dropdown list that are filled dinamically using the data of another dropdown or textbox, and the data of those drop down is used for another drop down, like
a chain, my problem rigth now is that the dropdown sometimes just have one answer/option, and if i try to select that option jquery doesn´t activate the $("#anything").change() event, 'cause i have just one option, how i handle it?
ahinestrosa1...
Member
61 Points
51 Posts
how to handle dropdownlist mvc/jquery
Mar 01, 2012 11:52 AM|LINK
i´m new developing on asp.net mvc, and even newer using jquery, so this is my problem: i have a few dropdown list that are filled dinamically using the data of another dropdown or textbox, and the data of those drop down is used for another drop down, like a chain, my problem rigth now is that the dropdown sometimes just have one answer/option, and if i try to select that option jquery doesn´t activate the $("#anything").change() event, 'cause i have just one option, how i handle it?
http://imageshack.us/photo/my-images/20/sinttulolp.jpg/
for example this i need the data of the red circle for filling the blue circle, but the red circle just have one option and if i click nothing happens
code:
$("#tbClave1").change(function() { if ($("#tbClave1").val().length == 4) { updateClave2($("#tbClave1").val()); } }); function updateClave2(cod) { var dd = document.getElementById("ddClave2"); dd.options.length = 0; dd.options[0] = new Option("Espere..."); dd.selectedIndex = 0; dd.disabled = true; codigo = cod; tipoArt = $("#ddTipoArticulo").val(); // Control de errores $("#ddClave2").ajaxError(function(event, request, settings) { dd.options[0] = new Option("No se Cargaron Datos"); }); // Obtenemos los datos... $.ajax({ url: '/Home/GetClave2', dataType: 'json', data: { words: codigo, tipoArticulo: tipoArt }, success: function(data, textStatus, jqXHR) { $.each(data, function(i, item) { dd.options[i] = new Option(item["clave2"], item["clave2"]); }); dd.disabled = false; } }); } controller: public ActionResult GetClave2(string words, string tipoArticulo){ List<Pedidos> mySource = miConexion.Clave2(username, password, words, tipoArticulo); return Json(mySource); } connection: public List<Pedidos> Clave2(string username, string password, string clave1, string tipoArticulo) { List<Pedidos> clave2 = new List<Pedidos>(); try { OleDbConnection conexion = new OleDbConnection (@"...."); OleDbCommand comandoSelect = new OleDbCommand(@" sql sentence " , conexion); comandoSelect.Connection = conexion; conexion.Open(); comandoSelect.ExecuteNonQuery(); OleDbDataReader resultado = comandoSelect.ExecuteReader(); while (resultado.Read()){ Pedidos miClave2 = new Pedidos(); miClave2.clave2 = resultado.GetString(0); clave2.Add(miClave2); } conexion.Close(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } return clave2; }saalameejen
Member
210 Points
50 Posts
Re: how to handle dropdownlist mvc/jquery
Mar 01, 2012 12:26 PM|LINK
check this cascading dropdown list
http://www.c-sharpcorner.com/uploadfile/2124ae/cascading-dropdown-list-in-Asp-Net-mvc-2-using-jquery/
ahinestrosa1...
Member
61 Points
51 Posts
Re: how to handle dropdownlist mvc/jquery
Mar 01, 2012 01:35 PM|LINK
but i initialize, and fill it different ... how i add that first value to the dropdown, and then start fill it in the next position?
i forgot tell u that i use mvc v1
How i initialize: ViewData["ddClave2"] = new SelectList(new[] { "(Selecciona)" }); how i deffine it: <% =Html.DropDownList("ddClave2") %>