iT邦幫忙

2022 iThome 鐵人賽

DAY 20
0
Software Development

C# ASP.NET MVC實作: 30天打造屬於你的網站應用程式系列 第 20

(DAY 20)C# ASP .NET MVC實作: 30天打造屬於你的網站應用程式-建立購物中心網站實際範例(八)

  • 分享至 

  • xImage
  •  

今天可以來把網站範例做一個小收尾啦~終於有個成果=W=

● Member/OrderList

首先在MemberController內加入OrderList()動作方法,如下面Code:

        public ActionResult OrderList()
        {
            string userId = User.Identity.Name;
            var orders = db.table_Order.Where(m => m.UserId == userId).OrderByDescending(m => m.Date).ToList();
            return View(orders);
        }

接著新增對應的View,加入時依照下圖選擇List範本與table_Order模型,版面配置頁也是使用_LayoutMember.cshtml

加入後自動產生檢視頁面,如下Code:

@model IEnumerable<DemoShoppingWebsite.Models.table_Order>

@{
    ViewBag.Title = "OrderList";
    Layout = "~/Views/Shared/_LayoutMember.cshtml";
}

<h2>OrderList</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.OrderGuid)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.UserId)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Receiver)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Email)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Address)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Date)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.OrderGuid)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.UserId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Receiver)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Email)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Address)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Date)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
}

</table>

接著修改一下部分內容:

  1. 第4行ViewBag.Title 與 第8行<h2>標籤內容改為「會員訂單列表」。

  2. 刪除第10-12行<p>標籤含內容,這幾行用不到。

  3. 將原本第57-59行<td>標籤內的內容刪除

    改加入顯示「訂單明細」的按鈕,Code如下:

<a href="@Url.Action("OrderDetail")?OrderGuid=@item.OrderGuid" class="btn btn-info">訂單明細</a>

這邊我們可以看到查詢訂單明細時,可以使用不會重複的OrderGuid參數來查詢,就不會有問題了。

完成後我們就可以來執行看看功能了,先隨意加入購物車商品,並填寫收件人資料。

按下「確認訂購」按鈕,就會移轉至訂單列表頁面,這邊會顯示已形成的訂單資料。因為資料庫檔案本身已經有加入幾筆訂單資料,所以也會一同顯示出來。

● Member/OrderDetail

最後是訂單明細的部份了,於MemberController內加入OrderDetail()動作方法,Code如下:

        public ActionResult OrderDetail(string OrderGuid)
        {
            var orderDetails = db.table_OrderDetail.Where(m => m.OrderGuid == OrderGuid).ToList();
            return View(orderDetails);
        }

接著新增對應的View,加入時依照下圖選擇List範本與table_OrderDetail模型,版面配置頁也是使用_LayoutMember.cshtml

加入後自動產生檢視頁面,如下Code:

@model IEnumerable<DemoShoppingWebsite.Models.table_OrderDetail>

@{
    ViewBag.Title = "OrderDetail";
    Layout = "~/Views/Shared/_LayoutMember.cshtml";
}

<h2>OrderDetail</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.OrderGuid)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.UserId)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ProductId)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Price)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Quantity)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.IsApproved)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.OrderGuid)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.UserId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ProductId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Price)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Quantity)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.IsApproved)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
}

</table>

修改一下部分內容:

  1. 第4行ViewBag.Title 與 第8行<h2>標籤內容改為「訂單明細」。

  2. 刪除第10-12行<p>標籤含內容,這幾行用不到。

  3. 將原本第62-66行的<td>標籤包含內容都刪除

完成後執行看看是否正常運作,進入會員訂單列表後,點選任一筆訂單的「訂單明細」按鈕,顯示畫面如下:

● 小結

到今天為止我們就算是完成一個小小的購物網站範例啦~不過相信大家會覺得還可以有改善的地方,或者加入新的功能,可以藉由這個範例再去修改延伸內容,大致就先講到這邊了。

明天我們要來將網站佈署上線,這樣就可以讓其他人透過網址連結也能看到自己做的網站啦~那我們就明天見!


上一篇
(DAY 19)C# ASP .NET MVC實作: 30天打造屬於你的網站應用程式-建立購物中心網站實際範例(七)
下一篇
(DAY 21)C# ASP .NET MVC實作: 30天打造屬於你的網站應用程式-網站佈署(一): Azure雲端帳號建立
系列文
C# ASP.NET MVC實作: 30天打造屬於你的網站應用程式30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
angeltsai32
iT邦新手 5 級 ‧ 2022-10-11 16:58:23

您好,想請問做到會員購物車清單,按下確認訂購後MemberController裡ShoppingCar的db.SaveChanges();會表示錯誤。麻煩大神幫忙解惑QQ

'/' 應用程式中發生伺服器錯誤。
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
描述: 在執行目前 Web 要求的過程中發生未處理的例外狀況。請檢閱堆疊追蹤以取得錯誤的詳細資訊,以及在程式碼中產生的位置。

例外狀況詳細資訊: System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

原始程式錯誤:
行 79: //更新資料庫,異動Order和OrderDetail
行 80: //完成訂單主檔和訂單明細的更新
行 81: db.SaveChanges();
行 82: return RedirectToAction("OrderList");
行 83: }

我看了下面這篇
https://dotblogs.com.tw/wasichris/2015/01/24/148255
可以在savechanges方法加上try catch 看例外訊息是如何?

我要留言

立即登入留言