I've been trying to update a flag (Active) in my database. To Do this i have a view that looks the following. In the view i have records in the databasee i am passing BillPayID in each row in the table in the view. WHat i want to is when a user presses the START or STOP Button the active flag is set to 0 or 1.
THE PROBLEM is, my view is ALWAYS sending the first BillPayID, even if a button on a different row is clicked.
Can some one please help?
@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>
}
}
this is the controller
[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);
}
Hidden fields still have a name and id in html, so in your case your putting x number of hidden fields for BillPayID that will all be named item.BillPayID.
Change the foreach loop to a for loop like so:
@for (int i =0; i < Model.Count(); i++)
{
@Html.HiddenFor(model => Model[i].BillPayID)
.... rest of your code
}
Your view code will send the entire model (IEnumerable) to your controller, which will make determining which row was selected a pain as it does not appear your setting any value in your model that tracks the selected item.
I would suggest you move your start/stop process to another view. If you click on an item in your table, it takes you to a second page that asks you to either start or stop. You can also use a javascript function to send the selected values to your controller.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.