addsw
addsw

Reputation: 87

Update Parent table from Child table in ASP.NET CORE

I have 2 tables; WorkSchedule(Parent) and WorkShiftBid(Child). Right now, I am able to update WSBidStatus to "Approved" when the button in my WorkShiftBid view page is pressed.

I have a status column (WorkScheduleStatus&WSBidStatus) in both tables and what i want is for example when the button is pressed, both Status will be updated to "Approved" where WorkScheduleID in both tables are the same.

How should my controller look like to do this?

Model:

namespace Website.Models
{
    public class WorkScheduleModel
    {
        [Key]
        public Guid WorkScheduleID { get; set; }

        public string WorkScheduleStatus { get; set; }
    }

    public class WorkShiftBidModel
    {
        [Key]
        public Guid WorkShiftBidID { get; set; }

        public string WSBidStatus { get; set; }

        public Guid WorkScheduleID { get; set; }
        [ForeignKey("WorkScheduleID")]
        public WorkScheduleModel WorkSchedules { get; set; }
    }
}

Controller:

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> WorkShiftBid(Guid? id)
        {
            SqlConnection con = new SqlConnection();
            SqlCommand com = new SqlCommand();
            SqlDataReader dr;

            connectionString();
            con.Open();
            com.Connection = con;
            com.CommandText = "update WorkShiftBid set WSBidStatus ='Approved' where WorkShiftBidID = @id ";
            com.Parameters.AddWithValue("@id", id);
            dr = com.ExecuteReader();

            var workShiftBidModel = await _context.WorkShiftBid.FindAsync(id);

            if (dr.Read())
            {
                con.Close();
                return RedirectToAction(nameof(WorkShiftBid));
            }
            else
            {
                con.Close();
                return RedirectToAction(nameof(WorkShiftBid));
            }

            void connectionString()
            {
                con.ConnectionString = ".........................";
            }
        }

View:

@model IEnumerable<WorkShiftBidModel>

<table class="table">
    <thead>
        <tr>
            <td> 
            <th>
                .
                .
                .
            </th>
            <form method="post">
                <input type="submit" class="btn btn-success btn-block" value="Approve" asp-route-id="@item.WorkShiftBidID" asp-action="WorkShiftTab" asp-controller="Manager" />
            </form>
            </td>
        </tr>
        }
    </tbody>
</table>

Upvotes: 1

Views: 416

Answers (1)

LudacrisX1
LudacrisX1

Reputation: 350

I don't have enough reputation to comment, but are you utilizing both Entity Framework and SqlCommands?

Nevertheless, you can find the Parent Key and update it via a SQL Command like so:

UPDATE WorkScheduleModel SET Status = 'Approved' WHERE Id = {ID}

Replace {Id} with the Key of your parent record.

Upvotes: 1

Related Questions