Pulling a return from a JS function to C #

0

In the user registration process I want to validate your CPF by JavaScript and return whether it is valid in beckend. And so make a if with this result to insert it in the database or not.

if (ValidaCpf(txtCpf.Text) == False){
    //retorna um erro
}

else{
   //cadastra o usuário
}

Note: I already have the function to validate the CPF and the Insert User method in the DB. I just need to pass the validation result from JavaScript to C #.

    
asked by anonymous 25.01.2018 / 17:11

1 answer

0

This is a simple example in Jquery for sending cpf.

var cpf = 000000000000 
    $("button").click(function () { 
                $.ajax({ 
                    url: "/Home/PostJson", //local para onde vai ser enviado
                    type: "POST", //forma de envio
                    contentType: "application/json", 
                    data: '{cpf:cpf}', //informação enviada
                    success: function (data) { 
                        alert(data);
                    } 
                }); 
            }) 

Now looking at the application side

 public class HomeController : Controller 
    {          
        [HttpPost] 
        public JsonResult PostJson([FromBody] string cpf) 
        { 
           //faz a validação do cpf

           return Json(VarResultado, JsonRequestBehavior.AllowGet);
        } 
    } 

Send the CPF by JSON to the side of the application, which does the validation and then returns with the result to the screen.

    
25.01.2018 / 19:36