【发布时间】:2020-07-10 17:29:29
【问题描述】:
我有 2 张桌子; WorkSchedule(父)和 WorkShiftBid(子)。现在,当我按下WorkShiftBid 视图页面中的按钮时,我可以将WSBidStatus 更新为“已批准”。
我在两个表中都有一个状态列 (WorkScheduleStatus&WSBidStatus),我想要的是例如当按下按钮时,两个状态都将更新为“已批准”,其中两个表中的 WorkScheduleID 都是一样的。
我的控制器应该如何执行此操作?
型号:
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; }
}
}
控制器:
[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 = ".........................";
}
}
查看:
@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>
【问题讨论】:
标签: c# sql-server entity-framework asp.net-core asp.net-core-mvc