Field ? null is not sending if empty

Asked

Viewed 105 times

0

I have this field:

<label asp-for="TransportadorID" class="col-md-3 control-label" style="text-align:left;"></label>
                <div class="col-md-2">
                    <input type="text" asp-for="TransportadorID" onkeypress="return BuscaDados1(event);" onblur="CarregaFornecedor1(this.value);" class="form-control" name="TransportadorID" id="idtransportador" />
                </div>

He’s the int type ? null, however always when giving the Ubmit it gives Focus to the field, I have already looked and there is no function that does this, what can be happening? This is in editing, in new the empty field works normal. I don’t know what could be going on. Any idea ?

Model:

  [Display(Name = "Transportador")]
    public int? TransportadorID { get; set; }

Function:

function BuscaDados1(e) {
var idfornecedor = document.getElementById("idtransportador").value;
if (OnEnter(e)) {
    if (idfornecedor == "?") { abreModalFornecedor1(); return false; }
    else {
        CarregaFornecedor1(idfornecedor);
        return false;
    }
}
else {
    return true;
}

}

function CarregaFornecedor1(id) {
            var url = "/PedidoVenda/CarregaTransportador";
            var id = $("#idtransportador").val();
            $.ajax({
                url: url
                , data: { id: id }
                , type: "POST"
                , datatype: "html"
                , success: function (data) {
                    console.log('entrou carrega fornecedor 1');
                    if (data.resultado > 0) {
                        $("#nometransportador").html(data.nomeFornecedor);
                    }
                    else {
                        if (id != "?" && id != "") {
                            alert('Transportador não encontrado, tente novamente.');
                            $("#idtransportador").val("");
                            $("#nometransportador").html("");
                        }

                    }
                }
            });
        }

Function CarregaTransportador

   [HttpPost]
    public ActionResult CarregaTransportador(int id)
    {
        try
        {
            var item = db.Fornecedores.Include(f => f.CategoriaFornecedores).Where(r => r.Id == id && r.CategoriaFornecedores.Transportador == true).Single();
            return Json(new
            {
                nomeFornecedor = item.Nome,
                ruaFornecedor = item.Rua,
                nFornecedor = item.Numero,
                bairroFornecedor = item.Bairro,
                cidadeFornecedor = item.Cidade,
                Resultado = item.Id
            });

        }
        catch { return Json(new { Resultado = 0 }); }
    }
  • 1

    has how to add the template, and functions js?

  • @Barbetta added.

  • You expect a NULL or an if (idfornecedor == "?") ... "?"

  • o ? opens a modal for search. and the void, is because it can be empty.

  • when you do the idfornecedor == "?" in vdd it expects inside the input to have text "?".

  • Yes, he expects you to have '?' because only with ? and enter, he opens the modal.

  • @Barbetta because the input type is text, so it accepts the ?

  • Aaah yes, I understand rsrs, he gets to enter the CarregaFornecedor1? if yes, what value the field var id are receiving?

  • @Barbetta he does get into CarregaFornecedor1, o campo id` is receiving, it is receiving empty.

  • have controller code /Pedidovenda/Loader? can add it, please

  • @Barbetta added.

  • @Barbetta managed to solve including the required directly in viewmodel, compiling and soon after removing the required.

  • @marianac_costa that crazy kkkkkk, but good that solved ;)

Show 9 more comments
No answers

Browser other questions tagged

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