Jquery Ajax Post (onchange infinite loop)

4

Good people I have the following problem: I have a dropdownList that depending on its value brings me by ajax a different form with its respective button, until there works well.

    <div id="contformularios1">

    </div>



      $('#dropVal').on('change',function (e) {

      //$("#contformularios1").html("");
       var tipo = $(this).val();

        if (tipo == "1") {

        //carga el formulario
        var url = "/ZsvalDatabase/Create";
        $.get(url, null, function (data) {
            var forms = $("#contformularios1").html(data);
        });
        $("#boton2").hide();
        $("#boton3").hide();
        $("#boton4").hide();
        $("#boton1").show();
    }

    if (tipo == '2') {
        $("#boton1").hide();
        $("#boton2").show();
        $("#boton3").hide();
       // carga al formulario
        var url = "/ZsvalWorkflow/Create";
        $.get(url, null, function (data) {
            $("#contformularios1").html(data);
        });
        }


    if (tipo == '3') {
        $("#boton1").hide();
        $("#boton2").hide();
        $("#boton3").show();
        // carga al formulario

        var url = "/ZsvalSearch/Create";
        $.get(url, null, function (data) {
            $("#contformularios1").html(data);
        });
    }

    //if (tipo == '4') {
    //    //var url = "/ZsvalVariable/Create";
    //    //$.get(url, null, function (data) {
    //    //    $("#contformularios1").html(data);
    //    //});
    //}


       })

The error occurs when I insert this data in sql with the following button:

    $("#boton1").click(function (ev) {

    var obj = {
        Id: 1,
        Cid: $("#cid").parents().children("select").val(),
        query: $("#query").parents().children("input").val(),
        Result: $("#result").parents().children("input").val(),
        Connection: null,
    }

    //envia el objeto
    $.ajax({
        type: 'POST',
        contentType: false,
        url: "/ZsvalDatabase/Create",
        dataType: 'json',
        contentType: "application/json; charset=utf-8",

        data: JSON.stringify(obj),
        success: function (data) {
            alert(data)

        },
    })
    ev.preventDefault();

})

Instead of inserting the Object only 1 time, it does so as many times as I have called different forms using dropdownlist . That is, if I select type 1, then I select type 2 and then again the 1 and the insert is generated a loop that inserts 3 or more objects

    public ActionResult Create(Zsvdb zsvdb)
    {
        if (ModelState.IsValid)
        {
            db.Zsvdb.Add(zsvdb);
            db.SaveChanges();
        return RedirectToAction("Index");
        }


        ViewBag.Cid = new SelectList(db.Connection, "Id", "Cname", zsvdb.Cid);
        //return Json(zsvdb, JsonRequestBehavior.AllowGet);
        return View(zsvdb);
    }

    
asked by Andromeda 27.01.2016 в 15:49
source

1 answer

2

It is that you should have two methods Create one that acts in GET and another one in POST

public ActionResult Create()
{
   //aqui accedes al contexto para recupera la lista
   return View(zsvdb);
}

[HttpPost]
public ActionResult Create(Zsvdb zsvdb)
{
   if (ModelState.IsValid)
   {
      db.Zsvdb.Add(zsvdb);
      db.SaveChanges();
   }
}

You will see that a method is defined as [HttpPost] then you separate the oeprations, when you make the GET you are not making the SaveChange()

    
answered by 27.01.2016 в 16:00