Precondition failed


#1

Hello,
I have converted a complex form in template form.
I have edited submit button calling updateXxxx method and setting id and Xxxx parameter.
I see that the c# backend got called but it returns 412- Precondition Failed.
How can I debug this thing? How can I discover which precondition fails?
It is an update so mandatory field are already filled.
Thanks,
Mario


#2

Hi Mario,

You can debug the .NET Core app using Visual Studio Code:
https://www.radzen.com/documentation/debugging/

Best Regards.
Vladimir


#3

Ok thanks I was able to debug.
I have reached the code below. It is executed two times, in the first one item is not null in the second time item is null and it returns a precondition failed.

[HttpPatch("{id}")]
public IActionResult PatchRicoco(int key, [FromBody]Delta<Models.RicocoDb.Ricoco> patch)
{
try
{
if(!ModelState.IsValid)
{
return BadRequest(ModelState);
}

        var items = this.context.Ricocos.Where(i=>i.id == key);
        
        items = EntityPatch.ApplyTo<Models.RicocoDb.Ricoco>(Request, items);

        var item = items.FirstOrDefault();

        if (item == null)
        {
            return StatusCode((int)HttpStatusCode.PreconditionFailed);
        }

        patch.Patch(item);

        this.OnRicocoUpdated(item);
        this.context.Ricocos.Update(item);
        this.context.SaveChanges();

Probably I am passing wrong parameters from javascript I send:

id ${ricoco.id} (tried also ${parameters.id}
Ricoco ${ricoco}


#4

Can you check what is triggering the second execution?


#5

Ok I am learning a lot but I need some clarifications.
I have discovered that the submit button alone without scripts is enough to trigger form save and to invoke the backend method PatchRicoco(...)
But looking at ricoco object in Visual Studio Code debug I see that the fields I have modified with gui are not updated.
Basically my real problem is that, when I click the submit button of the edit page, nothing happens.
In the debug I see many select queries are executed, I have not found update query.


#6

And, if I delete a mandatory field a warning "xxxx is required" appears immediately. But then I can click on submit and form get submitted anyway.


#7

Do you have any JavaScript errors in your browser console? Normally you cannot submit without filling the required fields.


#8

Ok I have debugged deeply.
First thing: I have discovered that, by mistake, I have put widgets outside template form definition.
Now I have put them again inside and things works better.
I have debugged the rest patch call and all parameters are correct.
Now here is the problem where I need help, here is the c# autogenerated code:

[HttpPatch("{id}")]
public IActionResult PatchRicoco(int key, [FromBody]Delta<Models.RicocoDb.Ricoco> patch)
{
try
{
if(!ModelState.IsValid)
{
return BadRequest(ModelState);
}

        var items = this.context.Ricocos.Where(i=>i.id == key);

Until the line above all is ok key is right and items is loaded with right content, but then:

items = EntityPatch.ApplyTo<Models.RicocoDb.Ricoco>(Request, items);

The line above destroys items content, so:

var item = items.FirstOrDefault();

        if (item == null)
        {
            return StatusCode((int)HttpStatusCode.PreconditionFailed);
        }

Item now is really null and I get a 412 precondition failed.

Can you help me?
Thanks,
Mario


#9

Hi Mario,

Can you post the Models.RicocoDb.Ricoco class definition?

Best Regards,
Vladimir


#10

Here it is:

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace RicocoNg.Models.RicocoDb
{
  [Table("ricoco")]
  public partial class Ricoco
  {
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int id
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public int? idStandardColoreFinito
    {
      get;
      set;
    }

    [ForeignKey("idStandardColoreFinito")]
    public StandardColore StandardColore { get; set; }
    [ConcurrencyCheck]
    public int? idScalaProduzione
    {
      get;
      set;
    }

    [ForeignKey("idScalaProduzione")]
    public Scala Scala { get; set; }
    [ConcurrencyCheck]
    public int idReparto
    {
      get;
      set;
    }

    [ForeignKey("idReparto")]
    public Reparto Reparto { get; set; }
    [ConcurrencyCheck]
    public int? additivo1
    {
      get;
      set;
    }

    [ForeignKey("additivo1")]
    public Additivi Additivi { get; set; }
    [ConcurrencyCheck]
    public int? additivo2
    {
      get;
      set;
    }

    [ForeignKey("additivo2")]
    public Additivi Additivi1 { get; set; }
    [ConcurrencyCheck]
    public int? additivo3
    {
      get;
      set;
    }

    [ForeignKey("additivo3")]
    public Additivi Additivi2 { get; set; }
    [ConcurrencyCheck]
    public int? additivo4
    {
      get;
      set;
    }

    [ForeignKey("additivo4")]
    public Additivi Additivi3 { get; set; }
    [ConcurrencyCheck]
    public int? additivo5
    {
      get;
      set;
    }

    [ForeignKey("additivo5")]
    public Additivi Additivi4 { get; set; }
    [ConcurrencyCheck]
    public int idUtente
    {
      get;
      set;
    }

    [ForeignKey("idUtente")]
    public Utenti Utenti { get; set; }
    [ConcurrencyCheck]
    public int idStandardColore
    {
      get;
      set;
    }

    [ForeignKey("idStandardColore")]
    public StandardColore StandardColore1 { get; set; }
    [ConcurrencyCheck]
    public int idStruttControllo
    {
      get;
      set;
    }

    [ForeignKey("idStruttControllo")]
    public Controllo Controllo { get; set; }
    [ConcurrencyCheck]
    public int? idScalaMescole
    {
      get;
      set;
    }

    [ForeignKey("idScalaMescole")]
    public Scala Scala1 { get; set; }
    [ConcurrencyCheck]
    public int idCartaRiferimento
    {
      get;
      set;
    }

    [ForeignKey("idCartaRiferimento")]
    public Cartum Cartum { get; set; }
    [ConcurrencyCheck]
    public int? pigmento1
    {
      get;
      set;
    }

    [ForeignKey("pigmento1")]
    public Pigmenti Pigmenti { get; set; }
    [ConcurrencyCheck]
    public int? pigmento2
    {
      get;
      set;
    }

    [ForeignKey("pigmento2")]
    public Pigmenti Pigmenti1 { get; set; }
    [ConcurrencyCheck]
    public int? pigmento3
    {
      get;
      set;
    }

    [ForeignKey("pigmento3")]
    public Pigmenti Pigmenti2 { get; set; }
    [ConcurrencyCheck]
    public int? pigmento4
    {
      get;
      set;
    }

    [ForeignKey("pigmento4")]
    public Pigmenti Pigmenti3 { get; set; }
    [ConcurrencyCheck]
    public int? pigmento5
    {
      get;
      set;
    }

    [ForeignKey("pigmento5")]
    public Pigmenti Pigmenti4 { get; set; }
    [ConcurrencyCheck]
    public int idCartaProduzione
    {
      get;
      set;
    }

    [ForeignKey("idCartaProduzione")]
    public Cartum Cartum1 { get; set; }
    [ConcurrencyCheck]
    public string capoProgetto
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string codiceColore
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string codiceMateriale
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public DateTime dataImmissione
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? finDL
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? finDa
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? finDb
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? finTollDL
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? finTollDa
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? finTollDb
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string linkExcel
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string masContrMescole
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? mesDL
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? mesDa
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? mesDb
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? mesTollDL
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? mesTollDa
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? mesTollDb
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string nomeColore
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string nomeMasterArticoloFinito
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string nomeProgetto
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string note
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string noteFinito
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string noteMescole
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string noteProduzione
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string numProgetto
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string numeroMasterArticoloFinito
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double pesoEsp
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double pesoSkin
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? prodDL
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? prodDa
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? prodDb
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? prodTollDL
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? prodTollDa
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? prodTollDb
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? valAdditivo1
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? valAdditivo2
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace RicocoNg.Models.RicocoDb
{
  [Table("ricoco")]
  public partial class Ricoco
  {
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int id
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public int? idStandardColoreFinito
    {
      get;
      set;
    }

    [ForeignKey("idStandardColoreFinito")]
    public StandardColore StandardColore { get; set; }
    [ConcurrencyCheck]
    public int? idScalaProduzione
    {
      get;
      set;
    }

    [ForeignKey("idScalaProduzione")]
    public Scala Scala { get; set; }
    [ConcurrencyCheck]
    public int idReparto
    {
      get;
      set;
    }

    [ForeignKey("idReparto")]
    public Reparto Reparto { get; set; }
    [ConcurrencyCheck]
    public int? additivo1
    {
      get;
      set;
    }

    [ForeignKey("additivo1")]
    public Additivi Additivi { get; set; }
    [ConcurrencyCheck]
    public int? additivo2
    {
      get;
      set;
    }

    [ForeignKey("additivo2")]
    public Additivi Additivi1 { get; set; }
    [ConcurrencyCheck]
    public int? additivo3
    {
      get;
      set;
    }

    [ForeignKey("additivo3")]
    public Additivi Additivi2 { get; set; }
    [ConcurrencyCheck]
    public int? additivo4
    {
      get;
      set;
    }

    [ForeignKey("additivo4")]
    public Additivi Additivi3 { get; set; }
    [ConcurrencyCheck]
    public int? additivo5
    {
      get;
      set;
    }

    [ForeignKey("additivo5")]
    public Additivi Additivi4 { get; set; }
    [ConcurrencyCheck]
    public int idUtente
    {
      get;
      set;
    }

    [ForeignKey("idUtente")]
    public Utenti Utenti { get; set; }
    [ConcurrencyCheck]
    public int idStandardColore
    {
      get;
      set;
    }

    [ForeignKey("idStandardColore")]
    public StandardColore StandardColore1 { get; set; }
    [ConcurrencyCheck]
    public int idStruttControllo
    {
      get;
      set;
    }

    [ForeignKey("idStruttControllo")]
    public Controllo Controllo { get; set; }
    [ConcurrencyCheck]
    public int? idScalaMescole
    {
      get;
      set;
    }

    [ForeignKey("idScalaMescole")]
    public Scala Scala1 { get; set; }
    [ConcurrencyCheck]
    public int idCartaRiferimento
    {
      get;
      set;
    }

    [ForeignKey("idCartaRiferimento")]
    public Cartum Cartum { get; set; }
    [ConcurrencyCheck]
    public int? pigmento1
    {
      get;
      set;
    }

    [ForeignKey("pigmento1")]
    public Pigmenti Pigmenti { get; set; }
    [ConcurrencyCheck]
    public int? pigmento2
    {
      get;
      set;
    }

    [ForeignKey("pigmento2")]
    public Pigmenti Pigmenti1 { get; set; }
    [ConcurrencyCheck]
    public int? pigmento3
    {
      get;
      set;
    }

    [ForeignKey("pigmento3")]
    public Pigmenti Pigmenti2 { get; set; }
    [ConcurrencyCheck]
    public int? pigmento4
    {
      get;
      set;
    }

    [ForeignKey("pigmento4")]
    public Pigmenti Pigmenti3 { get; set; }
    [ConcurrencyCheck]
    public int? pigmento5
    {
      get;
      set;
    }

    [ForeignKey("pigmento5")]
    public Pigmenti Pigmenti4 { get; set; }
    [ConcurrencyCheck]
    public int idCartaProduzione
    {
      get;
      set;
    }

    [ForeignKey("idCartaProduzione")]
    public Cartum Cartum1 { get; set; }
    [ConcurrencyCheck]
    public string capoProgetto
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string codiceColore
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string codiceMateriale
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public DateTime dataImmissione
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? finDL
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? finDa
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? finDb
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? finTollDL
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? finTollDa
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? finTollDb
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string linkExcel
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string masContrMescole
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? mesDL
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? mesDa
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? mesDb
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? mesTollDL
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? mesTollDa
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? mesTollDb
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string nomeColore
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string nomeMasterArticoloFinito
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string nomeProgetto
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string note
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string noteFinito
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string noteMescole
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string noteProduzione
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string numProgetto
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string numeroMasterArticoloFinito
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double pesoEsp
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double pesoSkin
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? prodDL
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? prodDa
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? prodDb
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? prodTollDL
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? prodTollDa
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? prodTollDb
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? valAdditivo1
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? valAdditivo2
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? valAdditivo3
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? valAdditivo4
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? valAdditivo5
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? valPigmento1
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? valPigmento2
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? valPigmento3
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? valPigmento4
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? valPigmento5
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string masContrFinito
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? zehMin
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? zehMax
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? mtgMin
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? mtgMax
    {
      get;
      set;
    }
  }
}valAdditivo3
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? valAdditivo4
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? valAdditivo5
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? valPigmento1
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? valPigmento2
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? valPigmento3
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? valPigmento4
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? valPigmento5
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string masContrFinito
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? zehMin
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? zehMax
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? mtgMin
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public double? mtgMax
    {
      get;
      set;
    }
  }
}

#11

Hi Mario,

You have enabled optimistic concurrency for your data source and the concurrency thinks that some of the fields are modified. Do you need optimistic concurrency in your case?
Best Regards,
Vladimir


#12

Thank you soo much the problem is solved.
I will reenable the optimistic concurrency in the future, we will see.