简体   繁体   English

如何遍历和编辑在 ASP.NET MVC C# 中选中复选框的数据库实体表中的记录

[英]How to loop through and edit records from database entity table that has a checkbox checked in ASP.NET MVC C#

I'm having a hard time getting this to work.我很难让这个工作。 I'm new to MVC and web development.我是 MVC 和 Web 开发的新手。 I've looked up lots of other examples but none work for me.我查找了许多其他示例,但没有一个对我有用。 Most either add new records or the multi-select checkboxes are for singular records.大多数要么添加新记录,要么多选复选框用于单个记录。 So what I want to accomplish is I have a form that has a list of all the records from the database table called 'ProductOrders'.所以我想要完成的是我有一个表单,其中包含来自名为“ProductOrders”的数据库表中的所有记录的列表。 I have a checkbox on each row for each individual product.对于每个单独的产品,我在每一行都有一个复选框。 I'd like to be able to go down the list and check whatever checkboxes I want whether it be one or many.我希望能够从列表中查看我想要的任何复选框,无论是一个还是多个。 Then when I'm done checking the checkboxes I need, when I hit submit it loops through the records and edits/updates the values of several of the columns from the table.然后,当我完成检查所需的复选框时,当我点击提交时,它会遍历记录并编辑/更新表中几个列的值。 For example, if I have 5 products from an order and I checked #1, #3, and #4, when submitted it would edit the values I specify from only those rows (1,3,& 4).例如,如果我有一个订单中的 5 个产品,并且我检查了 #1、#3 和 #4,那么在提交时它会编辑我仅从这些行(1、3 和 4)中指定的值。 I need to figure out how to get the "ProductOrderID" value of each row that's checked so it doesn't edit/update older posts in the table that have a checked checkbox value, it needs to be specific to that row where the checkbox is checked.我需要弄清楚如何获取选中的每一行的“ProductOrderID”值,这样它就不会编辑/更新表中具有选中复选框值的旧帖子,它需要特定于复选框所在的行检查。 Here's what I got so far, but it keeps telling me I don't have an object reference for the "foreach" part in the view.这是我到目前为止所得到的,但它一直告诉我我没有视图中“foreach”部分的对象引用。

View:看法:

   @model IEnumerable<AMS_ITAMSdb.Models.ProductOrder>
   @using AMS_ITAMSdb.Models
   @using AMS_ITAMSdb.Controllers

   @{
      ViewBag.Title = "ReturnOrder";
    }

    @using (Html.BeginForm())
    {
       <div class="title">
           <h4>Return Order</h4>
       </div>
       <p class="filter-section">
           <label class="filter-lbl1">Order #:&nbsp;</label>@Html.TextBox("orderNum", "", new { @class = "num" })<label class="lbl-txt">&nbsp;<i>(Type in order number here)</i></label>
           <input type="submit" value="Search" />&nbsp;
           <button type="button" onclick="location.href='@Url.Action("ReturnOrder")'">Reset</button>
       </p>
   }
   <table class="table">
       <tr>
           <th>
               Order #
           </th>
           <th>
               Product Code
           </th>
           <th>
               Qty Ordered
           </th>
           <th>
               Qty Available
           </th>
           <th>
               Return?
           </th>
           <th>
               New Qty Available
           </th>
       </tr>

       @foreach(var item in Model)
       {
           using(Html.BeginForm())
           { 
               <tr>
                 <td>
                    @Html.HiddenFor(modelitem => item.ProductOrderID)
                    @Html.DisplayFor(modelitem => item.OrderID)
                 </td>
                 <td>
                    @Html.DisplayFor(modelitem => item.Product_Code)
                 </td>
                 <td>
                    @Html.DisplayFor(modelitem => item.Qty_Ordered)
                 </td>
                 <td>
                    @Html.DisplayFor(modelitem => item.Qty_Available)
                 </td>
                 <td class="checkbox">
                    <input type="checkbox" id="@item.Returned" name="checkRec" value="true" />
                 </td>
                 <td>
                 </td>
               </tr>
           }
       }
   </table><br /><br />
              <div class="tbl-footer">
                 @using(Html.BeginForm("ReturnedOrder", "ProductOrders", FormMethod.Post))
                  {
                      <input type="submit" class="button" />
                  }
              </div><br /><br />

and my controller is:我的控制器是:

       //GET: ProductOrders/ReturnOrder
       public ActionResult ReturnOrder(string orderNum, ProductOrder productOrder)
       {
           if (!String.IsNullOrEmpty(orderNum))
           {
               //Filter results based on OrderID entered in textbox
               var prod = db.ProductOrders.Where(x => x.OrderID.ToString() == orderNum);
               return View(prod.ToList());
           }
           else
           {
               var product = db.ProductOrders.OrderBy(x => x.OrderID).Where(x => x.Returned == null);
               return View(product.ToList());
           }
       }

       //POST: ProductOrders/ReturnedOrder/
       [HttpPost]
       public ActionResult ReturnedOrder(ProductOrder productOrder, bool checkRec = false)
       {
            if (checkRec == true)
            {
                productOrder.Qty_Returned = productOrder.Qty_Returned;
                productOrder.Qty_Available = productOrder.Qty_Ordered + productOrder.Qty_Available;
                productOrder.Qty_On_Hand = productOrder.Qty_On_Hand - productOrder.Qty_Ordered;
                db.Entry(productOrder).State = EntityState.Modified;
                db.SaveChanges();

                Product product = db.Products.Where(x => x.ProductID == productOrder.ProductID).FirstOrDefault();
                product.Qty_Available = productOrder.Qty_Available;
                db.Entry(product).State = EntityState.Modified;
                db.SaveChanges();
            };                                            
            return View("ReturnOrder");
        }

Any help would be greatly appreciated as I've been stuck on this for quite some time and don't know where to go from here.任何帮助将不胜感激,因为我已经坚持了很长一段时间并且不知道从哪里开始。

Are you certain your controller is passing records to your view?您确定您的控制器正在将记录传递给您的视图吗? Try placing a break point at both returns from your controller and inspect your prod object.尝试在控制器的两个返回处放置一个断点并检查您的 prod 对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在表中保存记录后,如何循环遍历具有相同 ID 的所有记录并在 ASP.NET MVC C# 中更新它们 - After saving a record in a table, how to loop through all records with same ID and update them in ASP.NET MVC C# 通过C#在ASP.NET中未检查动态复选框 - Dynamic Checkbox is not getting checked in asp.net through C# 将选中的复选框存储在数据库ASP.NET C#的gridview中 - Store checked checkbox in gridview in database ASP.NET C# 如何检查asp.net mvc C#中复选框中选中的第一个项目# - How to check the first item that was checked in a checkbox in asp.net mvc C# 如何在Asp.net MVC C#中使用Linq从一个表中选择具有最大计数值的多个表中的记录# - how to select records from multiple table with max count value from one table using Linq in Asp.net MVC C# 如何使用实体框架在C#,ASP.NET MVC 3中的两个日期之间获取记录? - How to get records between two dates in C#, ASP.NET MVC 3 using Entity Framework? 选中复选框时 ASP.NET MVC 更改数据库值 - ASP.NET MVC Change Database value when checkbox is checked ASP.NET C#如何从数据库中填充表 - ASP.NET C# How to fill table from database 从C#asp.net中的数据库设置复选框值 - Set checkbox value from database in C# asp.net 如何在C#ASP.NET MVC项目中使用ajax更新SQL表中的记录顺序? - How to update orders of records in SQL table using ajax in C# ASP.NET MVC project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM