[Day29] 建立购物车系统 - 12

1. 在WebMvc专案新增购物车服务的功能

1.1 新增ViewComponent CartList的页面

在Views/Shared/Components目录,再新增CartList目录,最後加上Default.cshtml,也就是之前继承ViewComponent的CartList.cs会回传的页面,主要是显示购物车的内容:

@model WebMvc.Models.CartModels.Cart

@{
    ViewData["Title"] = "My Cart";
}

<div class="container-fluid">
    @if (TempData.ContainsKey("CartInoperativeMsg"))
    {
        <br />
        <div class="alert alert-warning" role="alert">
             @TempData["CartInoperativeMsg"]
        </div>
    }
    else
    {
        <div class="nes-table-responsive">
            <table class="nes-table is-bordered">
                <thead>
                    <tr>
                        <th>Picture</th>
                        <th>Product</th>
                        <th>Price</th>
                        <th>Quantity</th>
                        <th>Cost</th>
                    </tr>
                </thead>
                <tbody>
                    @for (int i = 0; i < Model.Items.Count; i++)
                    {
                        var item = Model.Items[i];
                        <tr>
                            <td><img class="" src="@item.PictureUrl" /></td>
                            <td>@item.ProductName</td>
                            <td>$ @item.UnitPrice.ToString("N2")</td>
                            <td>
                                <input type="hidden" name="@("quantities[" + i + "].Key")" value="@item.Id" />
                                <input type="number" class="" min="1" name="@("quantities[" + i + "].Value")" value="@item.Quantity" />
                            </td>
                            <td>
                                $ @Math.Round(item.Quantity * item.UnitPrice, 2).ToString("N2")
                                @if (item.OldUnitPrice != 0)
                                {
                                    <div class="alert alert-warning" role="alert"> Note that the price of this product changed in our Catalog. The old price when you originally added it to the cart was $ @item.OldUnitPrice </div>
                                }
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
        </div>

        <div class="container">
            <article class="row">
                <section class="col-xs-10"></section>
                <section class="col-xs-2">Total</section>
            </article>

            <article class="row">
                <section class="col-xs-10"></section>
                <section class="col-xs-2">$ @Model.Total()</section>
            </article>

            <article class="row">
                <section class="col-xs-7"></section>
                <section class="col-xs-2">
                    <button class="nes-btn is-primary" name="name" value="" type="submit">Update</button>
                </section>
                <section class="col-xs-3">
                    <input type="submit" class="nes-btn is-success" value="Checkout" name="action" />
                </section>
            </article>
        </div>
    }
</div>

1.2 新增Cart Index的页面

在Views新增Cart目录,再新增Index.cshtml,呼叫CartList ViewComponent和回前页的共通Header:

@using WebMvc.Services
@using WebMvc.ViewModels

@inject IAuthService<ApplicationUser> authService

@{
    ViewData["Title"] = "My Cart";
}

<form method="post" id="cartForm">
    <div class="">
        @await Html.PartialAsync("_Header", new List<Header>()
        {
            new Header(){Controller = "Catalog", Text = "Back to catalog"}
        })

        @await Component.InvokeAsync("CartList", new { user = authService.Get(User)})
    </div>
</form>

1.3 新增ViewModels的Header类别

在ViewModels新增Header.cs,指定要回到某一个Controller/显示文字的共通模型:

namespace WebMvc.ViewModels
{
    public class Header
    {
        public string Controller { get; set; }
        public string Text { get; set; }
    }
}

1.4 执行Debug

用VS执行所有3种Web Api和WebMvc,在用Dokcer执行cart.data和mssqlserver 2个服务,在首页点Add to cart,将会把商品加入到购物车,并在购物车页面可以修改数量,如图1

https://ithelp.ithome.com.tw/upload/images/20201015/20128651625W10Rl9Q.png

图1


<<:  [教学] ASP NET Core将HighChart图片插入到Word中并提供下载

>>:  学无止尽

【PHP Telegram Bot】Day20 - sendMessage:发送和转换 Markdown 讯息

今天先来点轻松的,先来看看各种 sendMessage 的功能,最後来转换使用者发送的 Markd...

Day 28:无法尽善尽美

这篇再谈谈 JUCE 的缺点。JUCE 论坛有一串讨论,谈到 LookAndFeel 的架构问题,J...

Day12.进入 ARM 世界: ARM Cortex-M Exception Behavior

Nested Interrupts Cortex-M3 和 NVIC 在硬体架构上支援(Nested...

【Day 09】Hook 的奇妙冒险 - Ring3 Hook

环境 Windows 10 21H1 Visual Studio 2019 x64dbg Aug 2...

如何自己设计一套ERP程序 前传-写ERP之前要决定的20件事 决定ERP後台资料库

第3个决定 决定ERP後台资料库 在所有资料库里笔者用过Oracle 和MS SQL,这2者使用时间...