Home > Mobile >  How to loop through and edit records from database entity table that has a checkbox checked in ASP.N
How to loop through and edit records from database entity table that has a checkbox checked in ASP.N

Time:07-21

I'm having a hard time getting this to work. I'm new to MVC and web development. 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'. 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). 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. 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.

View:

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

   @{
      ViewBag.Title = "ReturnOrder";
    }

    @using (Html.BeginForm())
    {
       <div >
           <h4>Return Order</h4>
       </div>
       <p >
           <label >Order #:&nbsp;</label>@Html.TextBox("orderNum", "", new { @class = "num" })<label >&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 >
       <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 >
                    <input type="checkbox" id="@item.Returned" name="checkRec" value="true" />
                 </td>
                 <td>
                 </td>
               </tr>
           }
       }
   </table><br /><br />
              <div >
                 @using(Html.BeginForm("ReturnedOrder", "ProductOrders", FormMethod.Post))
                  {
                      <input type="submit"  />
                  }
              </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.

CodePudding user response:

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.

  • Related