今天可以來把網站範例做一個小收尾啦~終於有個成果=W=
首先在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>
接著修改一下部分內容:
第4行ViewBag.Title 與 第8行<h2>標籤內容改為「會員訂單列表」。
刪除第10-12行<p>標籤含內容,這幾行用不到。
將原本第57-59行<td>標籤內的內容刪除
改加入顯示「訂單明細」的按鈕,Code如下:
<a href="@Url.Action("OrderDetail")?OrderGuid=@item.OrderGuid" class="btn btn-info">訂單明細</a>
這邊我們可以看到查詢訂單明細時,可以使用不會重複的OrderGuid參數來查詢,就不會有問題了。
完成後我們就可以來執行看看功能了,先隨意加入購物車商品,並填寫收件人資料。
按下「確認訂購」按鈕,就會移轉至訂單列表頁面,這邊會顯示已形成的訂單資料。因為資料庫檔案本身已經有加入幾筆訂單資料,所以也會一同顯示出來。
最後是訂單明細的部份了,於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>
修改一下部分內容:
第4行ViewBag.Title 與 第8行<h2>標籤內容改為「訂單明細」。
刪除第10-12行<p>標籤含內容,這幾行用不到。
將原本第62-66行的<td>標籤包含內容都刪除
完成後執行看看是否正常運作,進入會員訂單列表後,點選任一筆訂單的「訂單明細」按鈕,顯示畫面如下:
到今天為止我們就算是完成一個小小的購物網站範例啦~不過相信大家會覺得還可以有改善的地方,或者加入新的功能,可以藉由這個範例再去修改延伸內容,大致就先講到這邊了。
明天我們要來將網站佈署上線,這樣就可以讓其他人透過網址連結也能看到自己做的網站啦~那我們就明天見!
'/' 應用程式中發生伺服器錯誤。
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 看例外訊息是如何?