0

Hi currently I am doing a shopping cart for my project

I would like to ask how can I import the values in a ForEach to the database.

For example, I have the following data in my view.

@foreach (Cart_Has_Services c in Model)
    {
        <div class="cart-row">
            <div class="cart-items">@c.Cart_Service</div>
            <div class="cart-items">@c.Additional_Notes</div>
            <div class="cart-items">@c.Unit_Price</div>
            <div class="cart-items">
                <form asp-controller="Cart" asp-action="UpdateCart" formaction="post">
                    <input type="number" class="item-quantity-input" value="@c.Quantity" />
                    <input type="submit" class="btn btn-secondary" value="Update" />
                </form>
            </div>
            <div class="cart-items">
                <a asp-controller="Cart"
                   asp-action="DeleteItem"
                   asp-route-id="@c.Cart_Id"
                   onclick="return confirm('Delete Serivce @c.Cart_Service')">
                    Delete
                </a>
            </div>
        </div>
    }

As for now, I want to INSERT data (Cart Service, Additional Notes and Quantity) into my database (Order).

In my controller:

public IActionResult Checkout(Cart_Has_Services cart) 
    { 
    
        List<Cart_Has_Services> carts = DBUtl.GetList<Cart_Has_Services>("SELECT * FROM Cart");        
        string sql = @"INSERT INTO [Order](Order_Name,Order_Description,Order_Quantity) 
                     VALUES('{0}','{1}',{2})";
        int ord = DBUtl.ExecSQL(sql, cart.Cart_Service, cart.Additional_Notes, cart.Quantity);
        if (ord == 1)
        {
            TempData["Message"] = "Perofrmance Successfully Created";
            TempData["MsgType"] = "success";
            return RedirectToAction("Success");

        }
        else
        {
            ViewData["Message"] = DBUtl.DB_Message;
            ViewData["MsgType"] = "danger";
            return View("ShoppingCart");
        }
    }

I tried the method that I have inserted but it created without inserting the data.

How can I solve this problem?

Hope can get some guidance.

Thank you

mj1313
  • 7,930
  • 2
  • 12
  • 32
Yong Kevin
  • 33
  • 1
  • 7

1 Answers1

0

The form in the view only submit Quantity, without Cart_Service and Additional_Notes. To submit their value, you may set hidden inputs in the form. Also you should set name attribute for the input for model binding.

@foreach (Cart_Has_Services c in Model)
{
    <div class="cart-row">
        <div class="cart-items">@c.Cart_Service</div>
        <div class="cart-items">@c.Additional_Notes</div>
        <div class="cart-items">@c.Unit_Price</div>
        <div class="cart-items">
            <form asp-controller="Cart" asp-action="UpdateCart" formaction="post">
                
                <input type="hidden" name="Cart_Service" value="@c.Cart_Service" />
                <input type="hidden" name="Additional_Notes" value="@c.Additional_Notes" />
                <input type="number" name="Quantity" class="item-quantity-input" value="@c.Quantity" />
                <input type="submit" class="btn btn-secondary" value="Update" />
            </form>
        </div>
        <div class="cart-items">
            <a asp-controller="Cart"
               asp-action="DeleteItem"
               asp-route-id="@c.Cart_Id"
               onclick="return confirm('Delete Serivce @c.Cart_Service')">
                Delete
            </a>
        </div>
    </div>
}
mj1313
  • 7,930
  • 2
  • 12
  • 32
  • Thank you for your reply. How about my controller, do I have to edit anything? – Yong Kevin Jan 19 '21 at 15:12
  • Hi @YongKevin, does this controller action (`Checkout`) handle the request submitted from the form? Since the form target action is `UpdateCart`, what's the relationship between the view and this controller action? – mj1313 Jan 20 '21 at 07:02
  • The UpdateCart I already removed. Now, I only using ChecOut as my action to submit the form – Yong Kevin Jan 21 '21 at 01:32
  • @Yong Kevin, Ok, then you can set a breakpoint at the method, debug to see if `cart` accepts correct value, if it does, go ahead, check if the data inserts successfully. – mj1313 Jan 21 '21 at 02:06
  • Hi, I tried the method and is working but now I am facing another problem. Now, I would want to insert to a resolving table which I will need to retrieve the Id (Order_id etc.) but the Id is automatically INSERT, How can I retrieve the automatically generated ID (Order_Id) for my 2nd INSERT Statement – Yong Kevin Jan 21 '21 at 07:37
  • Hi @Yong Kevin, you can write a sql to select the latest instered record id, like [this](https://stackoverflow.com/a/5191525/11965297). – mj1313 Jan 21 '21 at 07:54
  • Hi @Yong Kevin, if this is solution is helpful, could you please [accept is as answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work), thanks! – mj1313 Jan 21 '21 at 08:30
  • Hi @mj131, I've tried this method, but it's different from mine because it has a Unique Id for every record (Order_id:1,2,3 etc.) – Yong Kevin Jan 21 '21 at 09:50