Jump to content

Select a dropdown option "AttemptedValue undefined"


wrb

Recommended Posts

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 by wrb
Link to comment
Share on other sites

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

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 by wrb
Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...