1. 程式人生 > 實用技巧 >小白白的ASP.NET訂單

小白白的ASP.NET訂單

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MODEL
{
    public class Order
    {
        public int OId { get; set; }
        public string productPno { get; set; }
        public int Ostate { get; set; }

        public string Productpno { get; set; }
        public int Num { get; set; }

        public string PName { get; set; }
        public string PImages { get; set; }
        public string PNo { get; set; }
        public Decimal Price { get; set; }
        public decimal Money { get; set; }
    }
}
Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MODEL;

namespace DAL
{
    public class OrderDAL
    {
        DBHelper db = new DBHelper();
        //新增訂單
        public int AddOrder(string productPno)
        {
            string sql = $"insert into [dbo].[Order] values('{productPno}',1)";
            return db.ExecuteNonQuery(sql);
        }
        //訂單顯示
        public List
<Order> GetOrders() { string sql = "select *,p.Price*s.Num Money from ShoppingCar s left join Product p on s.Productpno=p.PNo join [Order] o on s.Productpno=o.productPno"; return db.GetToList<Order>(sql); } } }
Dal
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MODEL;
using DAL;

namespace BLL
{
    public class OrderBLL
    {
        OrderDAL dal = new OrderDAL();
        //新增訂單
        public int AddOrder(string productPno)
        {
            return dal.AddOrder(productPno);
        }
        //訂單顯示
        public List
<Order> GetOrders() { return dal.GetOrders(); } } }
Bll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MODEL;
using BLL;

namespace Exercise_API.Controllers
{
    public class OrderController : ApiController
    {
        OrderBLL bll = new OrderBLL();
        //新增訂單
        [HttpPost]
        public IHttpActionResult AddOrder(string productPno)
        {
            return Ok(bll.AddOrder(productPno));
        }
        //訂單顯示
        [HttpGet]
        public IHttpActionResult GetOrder()
        {
            return Json(bll.GetOrders());
        }
    }
}
Conttroll
@{
    ViewBag.Title = "OrderShow";
}
<script src="~/Scripts/jquery-3.4.1.min.js"></script>
<h2>OrderShow</h2>
<script>


    $(function () {
        Show();
    })
    //載入購物車資料
    function Show() {
        $.ajax({
            url: "http://localhost:50262/api/Order/GetOrder",
            type: "get",
            dataType: "json",
            success: function (d) {
                console.log(d);
                $("#tb").empty();
                $(d).each(function () {
                    var list = '<tr>'
                    list += '<td style = "text-align:center" >' + this.OId + '</td >'    
                    list += '<td style = "text-align:center" >'                        
                    list += '<img src="' + this.PImages + '" width="100" height="80" /><br />'
                    list += '<span>' + this.Productpno + '</span> <br />'
                    list += '<span>' + this.PName + '</span>'
                    list += ' </td> '
                    list += '<td style = "text-align:center" > ' + this.Price + '</td > '                        
                    list += '<td style = "text-align:center" > ' + this.Num + '</td > '                        
                    list += '<td style = "text-align:center" > ' + this.Money + '</td > '  
                    if (this.Ostate==1) {
                        list += '<td style = "text-align:center" >代付款</td > ' 
                    }
                    else if (this.Ostate == 2) {
                        list += '<td style = "text-align:center" >已付款</td > '
                    }
                    else if (this.Ostate == 3) {
                        list += '<td style = "text-align:center" >代發貨</td > '
                    }
                    else {
                        list += '<td style = "text-align:center" >已完成</td > '
                    }                                                          
                    list += '</tr > '

                    $("#tb").append(list);
                })
            }
        })
    }
</script>

<table  border="1" class="table">
    <thead>
        <tr>
            <th>訂單序號</th>
            <th>商品資訊</th>
            <th>單價</th>
            <th>數量</th>
            <th>總價</th>
            <th>訂單狀態</th>
        </tr>
    </thead>
    <tbody id="tb">
        <tr>
            <td>1</td>
            <td>
                <img src="~/Images/006.png" width="100" height="80" /><br />
                <span>wb001</span><br />
                <span>小米手機</span>
            </td>
            <td>¥60</td>
            <td>1</td>
            <td>60</td>
            <td>代付款</td>
        </tr>
    </tbody>
</table>
Html