MVC4 视图未传递正确的模式值



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

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

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

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

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

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

最新更新