0

我一直在尝试更新数据库中的标志(活动)。为此,我有一个如下所示的视图。在视图中,我在数据库中有记录,我在视图中表的每一行中传递 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);
         }
4

1 回答 1

0

隐藏字段在 html 中仍然有名称和 ID,因此在您的情况下,您为 BillPayID 放置 x 个隐藏字段,这些字段都将被命名为 item.BillPayID。

将 foreach 循环更改为 for 循环,如下所示:

@for (int i =0; i < Model.Count(); i++)
{
  @Html.HiddenFor(model => Model[i].BillPayID)

  .... rest of your code
}

您的视图代码会将整个模型(IEnumerable)发送到您的控制器,这将使确定选择哪一行变得很痛苦,因为它不会显示您在模型中设置任何跟踪所选项目的值。

我建议您将启动/停止过程移至另一个视图。如果您单击表格中的某个项目,它会将您带到第二个页面,该页面要求您开始或停止。您还可以使用 javascript 函数将选定的值发送到您的控制器。

于 2013-10-21T23:22:04.697 回答