wrb Posted April 4, 2015 Share Posted April 4, 2015 (edited) Can anyone help? I have 2 DropDownList, when I select item 1 of the first segudo the loads as the corresponding sub-items. The problem is when I save, because the Drop second id appears as undefined. If I set a value for "id", "options += '<option value="1">' + val["descricao"] + '</option>';" the code works: What's wrong with "val"? ' + val["id"] + ' $("#cod_Prod_Med_Formula").change(function () { var dados = $(this).val().toString(); var PrincipioID = $("#Formula_Existente").val(); var options = ''; if (dados != 0) { $.ajax({ url: '@Url.Action("RetornaFormula", "Medicamento_Produto")', type: 'POST', dataType: 'json', data: { codigo: dados }, success: function (result) { $.each(result, function (key, val) { if (val["id"] != PrincipioID) options += '<option value="' + val["id"] + '">' + val["descricao"] + '</option>'; else options += '<option value="' + val["id"] + '" selected>' + val["descricao"] + '</option>'; }); $("#cod_Principio_Ativo").html(options); } }); } else { console.log(options) $("#cod_Principio_Ativo").html(options); } }).change();</script>CONTROLLER: [HttpPost]public ActionResult RetornaFormula(string codigo){var _Codigo = Convert.ToInt32(codigo);var query = from c in db.Prod_Med_Produto_Formulawhere c.id == _Codigoselect new { c.cod_Principio_Ativo, c.Prod_Med_Principio_Ativo.descricao};return Json(query);} Noting the console appears indefindo value: <div class="editor-field col-md-6"> <select name="cod_Principio_Ativo" class="form-control" id="cod_Principio_Ativo"> <option value="undefined">Febendazol</option> </select> </div> </div> Edited April 5, 2015 by wrb Link to comment Share on other sites More sharing options...
Ingolme Posted April 5, 2015 Share Posted April 5, 2015 I don't think anybody here speaks Portuguese. Please only make one thread for your question. If you need to change it, there's an "Edit" button at the bottom of the post. Link to comment Share on other sites More sharing options...
wrb Posted April 6, 2015 Author Share Posted April 6, 2015 (edited) The error is on the line "var PrincipioID = $ (" # Formula_Existente ") val ();.." I select the item in the DropDownList id = "cod_Prod_Med_Formula" and return to "descricao" in the DropDownList id = "cod_Principio_Ativo". What should I change this code? What should I implement so that the words "'+ val [" id "] +'" returns with "cod_Principio_Ativo"? Following classes: public partial class Prod_Med_Produto { public Prod_Med_Produto() public int id { get; set; } public Nullable<int> cod_Prod_Med_Formula { get; set; } public string nome_comercial { get; set; } public Nullable<int> cod_Principio_Ativo { get; set; } public virtual Prod_Med_Principio_Ativo Prod_Med_Principio_Ativo { get; set; } public virtual Prod_Med_Produto_Formula Prod_Med_Produto_Formula { get; set; } } public partial class Prod_Med_Produto_Formula { public Prod_Med_Produto_Formula() { this.Prod_Med_Produto = new HashSet<Prod_Med_Produto>(); } public int id { get; set; } public string formula { get; set; } public Nullable<int> cod_Principio_Ativo { get; set; } public virtual Prod_Med_Principio_Ativo Prod_Med_Principio_Ativo { get; set; } public virtual ICollection<Prod_Med_Produto> Prod_Med_Produto { get; set; } } Edited April 6, 2015 by wrb Link to comment Share on other sites More sharing options...
wrb Posted April 6, 2015 Author Share Posted April 6, 2015 Sorted out: $("#cod_Prod_Med_Formula").change(function () { var dados = $(this).val().toString(); var options = ""; if (dados != 0) { $.ajax({ url: '@Url.Action("RetornaPrincipio", "Medicamento_Produto")', type: 'POST', dataType: 'json', data: { codigo: dados }, success: function (result) { $.each(result, function (key, val) { options += '<option value="' + val["cod_Principio_Ativo"] + '" selected>' + val["descricao"] + '</option>'; }); $("#cod_Principio_Ativo").html(options); } }); } else { console.log(options) $("#cod_Principio_Ativo").html(options); } }).change(); Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now