Load input text after select

Asked

Viewed 145 times

0

I have a select that is being populated via ajax, when opening the modal, I call the function that loads the select, and it is working perfectly.

But I need a cascade effect, in the first field, and when changing, it changes the input text according to the selected field. How can I do it? I’m trying to do it this way:

function Carrega(id) {
    $.ajax({
        type: "post",
        url: "/PessoasServicos/CarregaDados",
        data: { id },
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            var tipoplano = $('#txtTipoPlano');
            tipoplano = data.resultado;
        }
    });
}

$('#cbplanos').on("click", function () {
    Carrega(1);
});

I’m passing the id 1 straight, to do a test, and here is the controller:

  [HttpPost]
    public ActionResult CarregaDados(int id)
    {
        try
        {
            var resultado = (from a in _context.PlanosServicos
                          where a.Id == id
                          select new
                          {
                              a.Tipo,
                          });

            return Json(resultado);
        }
        catch (Exception ex)
        {
            return Json(new { Result = ex.Message });
        }
    }

However it does not return me error, but also does not return me what I need.

  • It was not very clear, you want after selecting some item from select change the text of a input?

  • @Barbetta edited how I’m trying to do.

1 answer

1


I edited the method Carrega passing the id to the data and set the return within the input

function Carrega(id) {
    $.ajax({
        type: "post",
        url: "/PessoasServicos/CarregaDados",
        data: { id: id },
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            $("#txtTipoPlano").val(data.tipo)
        }
    });
}

Try calling the event directly on select, would look like this:

<select id="cbplanos" onchange="Carrega(this.value)"></select>

Finally, as you are selecting only one item, I played this value for a variable and then yes I returned a new one json with the countryside tipo

public ActionResult CarregaDados(int id)
{
    try
    {
        var resultado = _context.PlanosServicos.Where(p=> p.Id == id).FirstOrDefault().Tipo;

        return Json(new { tipo = resultado});
    }
    catch (Exception ex)
    {
        return Json(new { Result = ex.Message });
    }
}
  • I did exactly this way, but the value the id gets, is 0. It’s not getting the selected id.

  • I changed, try to call directly on select to function

  • I will try, but checking it is entering the controller catch, returning this error "Object Reference not set to an instance of an Object." Even changing to call in select, the same problem occurs.

  • Is getting some Id in the action?

  • In the action it reaches 0, but in the function it loads() it arrives right.

  • I found the problem rs, was as post, changed to get, and it worked, however the first value is not selected, only if I change in select.

  • Aaah yes. I can think of two options for the case of the first value; 1) Add an "Select" option 2) in the function who carries the select, after loading all items, call the Load method by passing the value of the selected item as parameter.

  • Incidentally, as I get the first value of select after include by ajax, so I can call this function right after.

  • 1

    You can do it like this var selecionado = $("#cbplanos option:selected").val(); - Take a look at this reply

  • 1

    It worked fine, thank you.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.