-1

I am having challenges on how to do bulk update in ASP.NET MVC.

I have the following view which is bound to the vwOrderCart model:

@model List<Kweb.Data.vwOrderCart>
@{
    ViewBag.Title = "Create Order";
}

This is a table on the view that iterates through the model and display data on the view:

foreach (var item in Model)
{
    <tr>
        <td>
            <input type="hidden" name="OrderCartId" value="@item.OrderCartId" />
              @item.ProductName
        </td>

        <td><input class="form-control" name='"OrderList["+ @item.OrderCartId +"].OrderPrice"' value="@string.Format("{0:C}", item.OrderPrice).Trim('$')" />
        </td>

        <td><input class="form-control" name='"OrderList["+ @item.OrderCartId +"].Qty"' value="@item.Qty" /></td>

        <td><input class="form-control" name='"OrderList["+ @item.OrderCartId +"].Discount"' value="@string.Format("{0:C}", item.Discount).Trim('$')" /></td>

        <td>@string.Format("{0:C}", item.TaxValue).Trim('$')</td>

        <td>@string.Format("{0:C}", item.TotalPrice).Trim('$')</td>


        <td><a href="#" onclick="deleteCart(@item.OrderCartId)" class="btn btn-sm default red-stripe"><i class="fa fa-times-circle"></i></a></td>

    </tr> 
}

And the following is my ActionResultController

[HttpPost]
public ActionResult Index(List<vwOrderCart> OrderList)
{
       
}

But the OrderList show null when I debug.

How do I send my controls on the form to the controller?

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
Brian Nkhata
  • 9
  • 1
  • 3
  • To submit a list from view to controller it's necessary to use indexing to be able the MVC binding to work properly. For example , see [How to pass List from view to controller - MVC 4](https://stackoverflow.com/a/39615356/6630084) – Jackdaw Sep 09 '22 at 15:20
  • Thanks, I can now get the values to controller. But how do I can only get the edited text values – Brian Nkhata Sep 09 '22 at 20:14
  • Can you add the `vwOrderCart` model declaration to the post? This can help to understand what the text values you are talking about. – Jackdaw Sep 09 '22 at 20:39

1 Answers1

0

Assuming this is you controller

 [HttpPost]
        public IActionResult UpdateOrder(List<vwOrderCart> OrderList)
{
    @foreach (item in OrderList)
    {
      var _orderlist = db.OrderCart.Find(item.Id);
      db.OrderList.Update(_orderlist);
    }

    db.SaveChanges();
}
Yat Fei Leong
  • 751
  • 1
  • 6
  • 10