0
我一直在嘗試更新我的數據庫中的標誌(活動)。要做到這一點,我有一個看起來如下所示。 在視圖中,我在數據庫中有記錄,我在視圖中的表中的每一行都傳遞了BillPayID。我想要的是當用戶按下開始或停止按鈕時,活動標誌設置爲0或1.MVC4查看不通過正確的模態值
問題是,我的視圖總是發送第一個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);
}