我一直在尝试更新数据库中的标志(活动)。为此,我有一个如下所示的视图。在视图中,我在数据库中有记录,我在视图中表的每一行中传递 BillPayID。我想要的是当用户按下开始或停止按钮时,活动标志设置为 0 或 1。
问题是,我的观点总是发送第一个 BillPayID,即使单击不同行上的按钮也是如此。
有人可以帮忙吗?
@if (Model !=null)
{
using (Html.BeginForm("StopScheduled", "Admin", FormMethod.Post, new { }))
{
<table>
<tr>
<th>
Account Number
</th>
<th>
Payee ID
</th>
<th>
Amount
</th>
<th>
Scheduled Date
</th>
<th>
Period
</th>
<th>
Active
</th>
<th>
Stop/Start Payment
</th>
</tr>
@foreach(var item in Model)
{
@Html.HiddenFor(model => item.BillPayID)
<tr>
<td>
@Html.DisplayFor(model => item.AccountNumber)
@Html.HiddenFor(model => item.AccountNumber)
</td>
<td>
@Html.DisplayFor(model => item.PayeeID)
@Html.HiddenFor(model => item.PayeeID)
</td>
<td>
$ @Html.DisplayFor(model => item.Amount)
@Html.HiddenFor(model => item.Amount)
</td>
<td>
@Html.DisplayFor(model => item.ScheduleDate)
@Html.HiddenFor(model => item.ScheduleDate)
</td>
<td>
@Html.DisplayFor(model => item.Period)
@Html.HiddenFor(model => item.Period)
</td>
<td>
@Html.DisplayFor(model => item.Active)
@Html.HiddenFor(model => item.Active)
</td>
<td>
@if (item.Active == 0)
{
<input type="submit" style="width:100%" value="START" />
}
else
{
<input type="submit" style="width:100%" value="STOP" />
}
</td>
</tr>
}
</table>
}
}
这是控制器
[HttpPost]
public ActionResult StopScheduled([Bind(Prefix = "item")] BillPayModel model)
{
int NewActive = 1;
if (model.Active == 1)
{
NewActive = 0;
}
try
{
if (ModelState.IsValid)
{
BillPay EditBillPay = db.BillPays.Find(model.BillPayID);
EditBillPay.Active = NewActive;
EditBillPay.ModifyDate = DateTime.Now;
db.Entry(EditBillPay).State = EntityState.Modified;
db.SaveChanges();
return View(model);
}
else
{
ModelState.AddModelError("", "Could not Stop Scheduled Payment");
}
}
catch (FormatException)
{
ModelState.AddModelError("", "Could not Stop Scheduled Payment");
}
return View(model);
}