如何将同一按钮指向不同页面?

时间:2019-05-03 22:45:51

标签: c# html sql asp.net-mvc razor

我试图将按钮order now引用到表中的每个不同产品。处理这种情况的最佳方法是什么?此刻,所有order now按钮每次都引用产品的第一条记录。但是我试图将每个不同列的数据发送到相应的order now按钮。最好的方法是什么?在这里您可以找到我对应的Database Tables

安慰者:


        public ActionResult PurchaseProduct()
        {
            if (Session["name"] == null)
            {
                return RedirectToAction("Index", "Customer");
            }

            var fetch = db.Products.FirstOrDefault();
            Session["pid"] = fetch.P_id;
            Session["pname"] = fetch.P_Name;


            return View(db.Products.ToList());
        }

        [HttpPost]
        public ActionResult Order(Product pt, Customer cr, int Quantity)
        {
            //available amount before order
            Session["p_name"] = db.Products.Where(x => x.P_id == pt.P_id).Select(x => x.P_Name).SingleOrDefault();
            Session["available_quantity"] = db.Products.Where(x => x.P_id == pt.P_id).Select(x => x.P_amount).SingleOrDefault(); 
            //amount needed
            Session["needed_quantity"] = Quantity;
            int a = Convert.ToInt32(Session["needed_quantity"]);
            int b = Convert.ToInt32(Session["available_quantity"]);
            if ( a <= b )
            {
                ViewBag.Message = "Order placed.";
                //quantity after the order has been placed
                int final_quantity = b - a;
                //total price (amount needed*price)
                Session["total_price"] = db.Products.Where(x => x.P_id == pt.P_id).Select(x => x.P_price).SingleOrDefault();
                int total_price = Convert.ToInt32(Session["total_price"]) * a;
                //amount after the order
                Session["final_quantity"] = db.Products.Where(x => x.P_id == pt.P_id).Update(x => new Product { P_amount = final_quantity });
                Session["c_id"] = db.Customers.Where(x => x.C_Id == cr.C_Id).Select(x => x.C_Id).SingleOrDefault();
                int c_id = Convert.ToInt32(Session["c_id"]);
                Session["p_id"]= db.Products.Where(x => x.P_id == pt.P_id).Select(x => x.P_id).SingleOrDefault();
                int p_id = Convert.ToInt32(Session["p_id"]);
                //adding record to table 'order'
                string p_name = Session["p_name"].ToString();
                Session["Add_Order"] = db.Orders.Add(new Order {O_name = p_name, O_Price = total_price
                , O_amount = a ,C_id = c_id, P_id = p_id});
                db.SaveChanges();

            }
            else
            {
                ViewBag.Message = "Order can't be placed, product amount is " + b.ToString();
            }

            return View();
        }

产品概述页面

@model IEnumerable<IMS.Models.Product>

        @{
            foreach (var item in Model)
            {
                <tr>
                    <td>@item.P_Name</td>

                    <td>@item.P_size</td>

                    <td>@item.P_price</td>

                    <td><img src="/images/@item.P_Image" width="200" height="200" /></td>
                    <td><a href="@Url.Action("Order", "CHome", new { id=@item.P_id})" class="btn btn-success">Order Now</a></td>
                </tr>
            }
        }

    </table>

    </div>

点击order now

后,每个产品的页面
<div class="form-group">
                @{
                    using (Html.BeginForm())
                    {

                        <p style="color:red;">@ViewBag.Message</p>
                        <div class="form-group">
                            <label>Product name: </label>
                            <input type="text" name="pname" id="pname" value="@Session["pname"]" class="form-control" readonly />

                        </div>

                        <div class="form-group">
                            <label>Customer id: </label>
                            <input type="text" name="c_id" id="c_id" value="@Session["id"]" class="form-control" readonly />

                        </div>

                        <div class="form-group">
                            <label>Product id: </label>
                            <input type="text" name="p_id" id="p_id" value="@Session["pid"]" class="form-control" readonly />

                        </div>
                        <div class="form-group">
                            <label>Amount: </label>
                            <label>Available amount: @Session["available_quantity"] </label>
                            <input type="text" name="Quantity" class="form-control" />

                        </div>

                        <input type="submit" value="Submit" name="submit" class="btn btn-success" />

0 个答案:

没有答案