简体   繁体   中英

Cannot get one of my put requests to work (getting CORS error)

I am stumped on getting my relatively simple PUT request to work properly. In my controller, I have an edit(Put) method, which works fine, however, I have a second PUT request that is used, as a drag and drop to reorder the steps in a checklist, this method is getting CORS error/ HTTP 500 when I try to run it in either angular or Postman. I am stumped how one routine is working, but the other is erroring out

I've tried debugging, and verifying the data is being passed correctly to the controller action, but it is not saving to the database, after it hits my foreach statement, then save, it bombs out

Here is the code for editing, which works

[HttpPut("{stepId}")]
public async Task<ActionResult> EditStep(int stepId, LogChecklistSteps step)
{        
    step.Idstep = stepId;
    _repo.EditStep(step);
    var history = FileHistory(step, "Draft", "Edited a Step");

    _repo.Add(history);
    await _repo.SaveAll();
    return NoContent();
}

Here is the code that I am having problems with

[HttpPut("reorder")]
public async Task<ActionResult> ReorderSteps(LogChecklist checklist)
{
    foreach(var s in checklist.LogChecklistSteps)
    {
        _repo.ReorderSteps(s);
    }

    await _repo.SaveAll();
    return NoContent();
}

and the code in my repo

public void EditStep(LogChecklistSteps step )
{
    _ctx.Entry(step).State = EntityState.Modified;
}

public void ReorderSteps(LogChecklistSteps step)
{
    _ctx.LogChecklistSteps.Update(step);
}

public async Task<bool> SaveAll()
{
    return await _ctx.SaveChangesAsync() > 0;
}

Turns out my issue was my scaffolded dbcontext had my Step field listed as a composite key. This project is for an old database, that the person who designed it, used composite keys rather than unique ids, I fixed that in the test system, once I removed that field from the modelbuilder, my code started working.

Thank you all for looking at my issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM