Function not defined with javascript

Asked

Viewed 1,709 times

2

I made a cshtml and in it there is a button that calls my function that inserts in the BD. It turns out that when I click the button, it gives me this error: Uncaught Referenceerror: Insert registration is not defined. What do I need to do to get around this error? I thought that nothing else was needed, because the button calls the function. See my code: My cshtml:

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Cadastro</title>
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <link href="~/Content/bootstrap.css" rel="stylesheet" />
    <script src="~/Scripts/bootstrap.min.js" type="text/javascript"></script>
    <script src="~/Scripts/bootstrap.js" type="text/javascript"></script>
    <script src="~/Scripts/Util.js"></script>
</head>
<body>
    <form role="form" class="">
        <div class="form-group col-xs-4 col-sm-4 col-md-4 col-lg-4">
            <label for="txtNmCadastro" class="col-sm-4 control-label">Nome completo</label>
            <input type="text" class="form-control col-lg-2" id="txtNmCadastro" name="nome" placeholder="Digite seu nome completo">
        </div>

        <div class="form-group col-xs-4 col-sm-4 col-md-4 col-lg-4">
            <label for="txtEmail" class="col-sm-4 control-label">Email</label>
            <input type="email" class="form-control col-lg-2" id="txtEmail" name="email" placeholder="Digite seu e-mail válido">
        </div>

        <div class="form-group col-xs-4 col-sm-4 col-md-4 col-lg-4">
            <label for="txtEndereco" class="col-sm-4 control-label">Endereço</label>
            <input type="text" class="form-control col-lg-2" id="txtEndereco" name="Endereco" placeholder="Digite seu endereço">
        </div>
        <div class="form-group col-xs-4 col-sm-4 col-md-4 col-lg-4">
            <label for="txtBairro" class="col-sm-4 control-label">Bairro</label>
            <input type="text" class="form-control col-lg-2" id="txtBairro" name="Bairro" placeholder="Digite o bairro">
        </div>

        <div class="form-group col-xs-4 col-sm-4 col-md-4 col-lg-4">
            <label for="txtCidade" class="col-sm-4 control-label">Cidade</label>
            <input type="email" class="form-control col-lg-2" id="txtCidade" name="Cidade" placeholder="Digite a cidade">
        </div>

        <div class="form-group col-xs-4 col-sm-4 col-md-4 col-lg-4">
            <label for="txtTelefone" class="col-sm-4 control-label">Telefone</label>
            <input type="text" class="form-control col-lg-2" id="txtTelefone" name="Telefone" placeholder="Digite o número do seu telefone">
        </div>

        <div class="form-group col-xs-4 col-sm-4 col-md-4 col-lg-4">
            <label for="txtCelular" class="col-sm-4 control-label">Celular</label>
            <input type="text" class="form-control col-lg-2" id="txtCelular" name="Celular" placeholder="Digite o número do seu celular">
        </div>

        <div>
            <input id="btnGravarCadastro" type="button" class="btn btn-success" value="Gravar" onclick=" return InsereCadastro();" />
        </div>
        </form>
</body>
</html>

My file . js(jquery)

function InsereCadastro() {

    var resultado = jQuery.parseJSON('{ "nmcadastro": "' +$("#txtNmCadastro").val() + '" , "email_cadastro": "' + $("#txtEmail").val() + '" , "end_cadastro": "' + $("#txtEndereco").val() 
                                        + '" , "bairro_cadastro": "' + $("#txtBairro").val() + '" , "cidade_cadastro": "' + $("#txtCidade").val() + '" , "uf_cadastro": "'+ $("#cbxUf").val() 
                                        + '" , "tel_cadastro": "' + $("#txtTelefone").val() + '" , "cel_cadastro": "' + $("#txtCelular").val() +'" }');

    $.ajax({
        url: '/Cadastro/InsereCadastro',
        datatype: 'json',
        contentType: "application/json; charset=utf-8",
        type: "POST",
        data: JSON.stringify({ _cadastro: resultado }),
        success: function (data) {

            alert('Registro gravado com sucesso.');

        },
        error: function (error) {

            alert('Erro ao tentar gravar o registro.');
        }

    });
}

The code of my controller

[HttpPost]
        public void InsereCadastro(tbl_cadastro _cadastro)
        {

           using (INETGLOBALEntities db = new INETGLOBALEntities())
           {
               tbl_cadastro tb = new tbl_cadastro();

               try
               {
                   tb.nmcadastro = _cadastro.nmcadastro;
                   tb.email_cadastro = _cadastro.email_cadastro;
                   tb.end_cadastro = _cadastro.end_cadastro;
                   tb.bairro_cadastro = _cadastro.bairro_cadastro;
                   tb.cidade_cadastro = _cadastro.cidade_cadastro;
                   tb.uf_cadastro = _cadastro.uf_cadastro;
                   tb.tel_cadastro = _cadastro.tel_cadastro;
                   tb.cel_cadastro = _cadastro.cel_cadastro;

                   db.tbl_cadastro.Add(tb);
                   db.SaveChanges();
               }
               catch (Exception ex) 
               {
                   Erro = "Erro na gravação do registro: " + ex.Message;
               }

           }
  • 1

    what is the name of the file where the function is InsereCadastro?

1 answer

3


There are two possibilities

  • The function really was not declared, you forgot to call your javascript file
  • Is there an error in your javascript file before you declare the function

You have placed this function in your Util.js file?

Browser other questions tagged

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