save in the database with ajax?

0

Hello developers, I am trying to save data in my database using ajax, but at the moment of running my app it seems to omit the portion of the code that would allow me to save the data, this is the code I am using.

<script>
$(".clMotivo").click(function (event) {
            var id = $(this).attr("data-id");
            var data = $(this).serialize();
            var gafete = $("txtGafete").val();
            var datos = {};
            datos = {
                motivo_Id: id,
                gafete: gafete
            };
            $.ajax({
                url: "http://localhost:49851/api/visita/agregar",
                method: "POST",
                data: JSON.stringify(datos),
                contentType: "aplication/json",
                success: function (data) {
                    alert(data);
                    ocultarPopUpAcceso();
                }
            });            
        });
</script>

will be that I lack something in this same code or it will be that I must serialize the json in the controller did not take much programming in mvc and that's why I have that doubt I hope you can understand my point and help me a little

    
asked by Reyes 04.04.2018 в 03:06
source

3 answers

0

I had an error in txtGafete that prevented entering the ajax below I show the code that I already corrected and functional thanks to everyone for their contributions.

<script>
$(".clMotivo").click(function (event) {
            var id = $(this).attr("data-id");
            var gafete = $("#txtGafete").val();
            var datos = {};
            datos = {
                motivo_Id: $(this).attr("data-id")id,
                gafete: $("#txtGafete").val()
            };
            $.ajax({
                url: "http://localhost:49851/api/visita/agregar",
                method: "POST",
                data: JSON.stringify(datos),
                contentType: "application/json",
                success: function (data) {
                    alert(data);
                    ocultarPopUpAcceso();
                }
            });            
        });
</script>
    
answered by 04.04.2018 / 22:15
source
0

It's probably because of your url, it's better to work with relative url than to put it completely; in MVC normally controller / function is placed. You can also summarize your data a bit.

<script>
 $(".clMotivo").click(function (event) {
                $.ajax({
                    method: "POST",
                    contentType: "json",
                    url: "/visita/agregar",
                    data: {
                            motivo_Id: $(this).attr("data-id"),
                            gafete: $("txtGafete").val()
                   },
                    success: function (data) {
                        alert(data);
                        ocultarPopUpAcceso();
                    }
                });            
            });
    </script>

Debug your giving a breakpoint on your controller and make sure they are entering the add feature.

    
answered by 04.04.2018 в 03:20
0

You should place the code of the action in the controller that will process the information, only with the JS that you are using is not enough. Your URL link refers to an action in a Controller For example:

public class HomeController: Controller{

   private IDataService servicio;

   public HomeController(IDataService service){
        servicio = service;
   }

   Route["/visita/agregar"]
   public string Post(int motivo_Id, string gafete){
     servicio.guardar(motivo_Id, gafete)
     //retornar mensaje de respuesta al llamado ajax
   }

}

Ideally your controller should not include the logic of saving in database, that would violate the Single Responsibility Principle so you should delegate it to another class. To save in database you have several options. among them this ADO. NET although I recommend you take a look at Dapper.

link

link

I hope it helps you.

    
answered by 04.04.2018 в 16:14