This question is regarding updating current record in the database table and adding new record.
In my asp.MVC application, from the view I have passed the relavent data to the controller, Here is my database table and highligted the relevent record to be updated.
This is my controller, and there can see the data passed from the view.
So as the picture 1, ApprovalProcess_Id 40 should update with the today date, approval_status should be 1, Approval_note should update as well. As you can see picture 3 Note data is already there. So Approval_ProcessId is I got seperately to the tempdata.
int tempAId = int.Parse(TempData.Peek("CurrentId").ToString());
The way I tried as the picture, isn't updating either, I want to update the note, approvestatus, datetime at the current record and need to add new record with the same tempAID with the model containing the new approver_Id.
This is my current code
public ActionResult ApproveRequest(ApprovalProcess approvalProcess)
{
if (ModelState.IsValid)
{
if (approvalProcess.Approvers != null)
{
int tempAId = int.Parse(TempData.Peek("CurrentId").ToString());
ApprovalParty approvalParty = new ApprovalParty();
approvalParty.Approved_Date = DateTime.Now;
approvalParty.Approved_Note = approvalProcess.Approvers.ToList().First().Approved_Note;
approvalParty.Approve_Status = true;
}
//approvalProcess approvalProcess1 = new approvalProcess();
//approvalParty.Approved_Date = DateTime.Now;
//approvalParty.Approve_Status = true;
//ApprovalParty model = new ApprovalParty();
////approvalParty.ApprovalProcess_Id = tempAId;
//approvalParty.Approved_Note = model.Approved_Note;
//db.SaveChanges();
return RedirectToAction("Index");
}
else
{
return View("Index");
}
}
CodePudding user response:
You're not doing any updates on the database at all which is to say you're updating all the properties in the world, but none of it is being pushed back to your database.
Here's an example that would push an update to your database:
public ActionResult ApproveRequest(ApprovalProcess approvalProcess)
{
if (ModelState.IsValid)
{
int.TryParse(TempData.Peek("CurrentId").ToString(), out int tempAId);
// This pulls the record from your db so that entity framework can track and update it.
var approvalParty = db.ApprovalParties.Find(tempAId);
approvalParty.Approval_Date = DateTime.Now;
// This will error because you'd need to select a particular Approver in the list to determine which Approved_Note to use.
// Should be something like Approvers.ToList()[0].Approved_Note
approvalParty.Approved_Note == approvalProcess.Approvers.ToList().Approved_Note;
approvalParty.Approve_Status = true;
// Pushes the updated data to the database.
db.SaveChanges();
}
}
For adding a new ApprovalParty record (I'm just assuming this is what you're calling it, you'll do the property value mapping to a new ApprovalParty and add it to db.ApprovalParties
:
var approvalParty = new ApprovalParty
{
Approved_Date = DateTime.Now,
Approved_Note = approvalProcess.Approvers.ToList()[0].Approved_Note,
Approved_Status = true;
};
db.ApprovalParties.Add(approvalParty);
db.SaveChanges();