When inserting an array in Sql Server the assigned Id messes up the array [closed]

1

Sorry for the title but I did not know how to describe the situation. The situation is as follows: I have a project in VS2017, asp.net core where I have a method that enters an array into the database. It is an array of a table that I created with Jquery that is a payment plan for a client's debt.

VIEW

<script>
    $("#boton").click(function () {
        var filas = $("#cuotas").val();
        var monto = $("#montocuota").val();
        var dni = $("#dni").val();
        var fecha = $("#fechavto").val();
        var tabla = "";
        var result = $("#tablaplan");
        tabla += "<table class=table table-hover>";
        tabla += "<thead>";
        tabla += "<tr>";
        tabla += "<th>Cuota N°</th>";
        tabla += "<th>Monto</th>";
        tabla += "<th>Fecha Vto</th>";
        tabla += "<th>Documento</th>";
        tabla += "</tr>";
        tabla += "</thead>";
        tabla += "<tbody>";
        for (var i = 0; i < filas; i++) {
            var vto = moment(fecha).add((i), 'month').format("YYYY-MM-DD");
            tabla += "<tr>";
            tabla += "<td>" + "<input name=[" + i + "].Cuota value=" + (i + 1) + " class=form-control input-sm readonly style=width:100px></input>" + "</td>";
            tabla += "<td>" + "<input name=[" + i + "].Monto_cuota value=" + monto + " class=form-control input-sm readonly style=width:100px></input>" + "</td>";
            tabla += "<td>" + "<input name=[" + i + "].Fecha_vto value=" + vto + " class=form-control input-sm readonly style=width:150px></input>" + "</td>";
            tabla += "<td>" + "<input name=[" + i + "].Dni value=" + dni + " class=form-control input-sm readonly style=width:150px></input>" + "</td>";
            tabla += "<td>" + "<input name=[" + i + "].Estado value=Activo class=form-control input-sm readonly style=width:150px type=hidden></input>" + "</td>";
            tabla += "</tr>";
        }

        tabla += "</tbody>";
        tabla += "</table>";
        result.html(tabla);
    });

</script>

CONTROLLER

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create([Bind("Fecha,Cuotas,MontoCuota,Estado,Dni,Total")] PlanPagos planPagos, string Dni, ICollection<DetallePlanPagos> detallePlanPagos)
            {

//verifico si tiene plan activo               
var query = from d in _context.Deudores
            join p in _context.PlanesPagos on d.Dni equals p.Dni
            where p.Estado == "Activo" && p.Dni == Convert.ToInt32(Dni)
            select p;
var cuenta = query.Count();

                if (ModelState.IsValid)
                {
                    if (cuenta == 0)
                    {

                        _context.DetallesPlanesPagos.AddRange(detallePlanPagos);
                        _context.Add(planPagos);
                        foreach(var item in detallePlanPagos)
                        {
                            //asigno FK
                            item.PlanPagosId = planPagos.Id;
                        }
                        await _context.SaveChangesAsync();
                        return View("Success");


                    }
                    else
                    {
                        ViewData["mensaje"] = string.Format("Error!. El deudor con Dni {0} ya" +
                            " tiene una refinanciación activa. Deberá anularla para cargar otro Plan de Pagos.", Dni);
                        return View("ErrorAlProcesar", ViewData["mensaje"]);
                    }

                }
                return View(planPagos);
            }

While everything works well, the problem arises that for example, if I put many quotas, more than 15 in the database inserts them in this way (screenshot of the table in the database ):

As you can see the Id is perfect, but the order of the fee is messy. From quota number 1 it goes to 17 instead of quota number 2. In other cases where I enter a payment plan of, for example, 5 installments, I have no problem.

Any suggestions? Thank you very much

    
asked by German W 06.07.2018 в 04:24
source

1 answer

0

I already solve it. Before inserting order by quota number. So it is canceled by order of quota in the correct order. Greetings and thanks. I add the code that made it work:

_logger.LogInformation("Recibo creado por Usuario: {ID}.", User.Identity.Name);
_context.Add(recibo);


//agrego Id al recibo                            
var detalleId = _context.DetallesPlanesPagos.Where(o => o.Dni.Equals(Convert.ToInt32(Dni)) && o.NRecibo == 0).OrderBy(x => x.Cuota).FirstOrDefault();
recibo.DetallePlanPagosId = detalleId.Id;

//guardo cambios
await _context.SaveChangesAsync();
await ActDetallePago(Convert.ToInt32(Dni));
return View("Success");
    
answered by 06.07.2018 / 16:18
source