ASP.NET - MVC - Phương thức Details
Tìm hiểu phương thức Details Và View Details
Bây giờ, chúng ta ta chuyển sang tìm hiểu phương thức Details cũng như View của nó.
1. Tùy chỉnh lại View
Từ trang Index.cshtml. Sau đó, nhấp liên kết Xem trên giao diện Web để đi đến trang xóa với liên kết http://localhost:xxxx/Book/Details/13:
Ngoài việc tùy chỉnh tiếng Anh thành tiếng Việt cho Form xóa ta còn tùy chỉnh một số thẻ sau:
Để hiển thị hình chúng ta sẽ thay đổi @Html.DisplayFor(model => model.Images) thành <img src="~/bookimages/@Model.Images" />
<dd>
<img src="~/bookimages/@Model.Images" />
</dd>
Ở phần mô tả nội dung hiển thị luôn các thẻ html. Chúng ta sẽ đổi @Html.DisplayFor(model => model.Description) thành @Html.Raw(Model.Description)
<dd>
@Html.Raw(Model.Description)
</dd>
Mã nguồn trang Details.cshtml sau khi tùy chỉnh:
@model BookStoreManager.Models.Book
@{
ViewBag.Title = "Details";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Thông tin chi tiết sách</h2>
<div>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Title)
</dt>
<dd>
@Html.DisplayFor(model => model.Title)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Price)
</dt>
<dd>
@Html.DisplayFor(model => model.Price)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Images)
</dt>
<dd>
<img src="~/bookimages/@Model.Images" />
</dd>
<dt>
@Html.DisplayNameFor(model => model.Description)
</dt>
<dd>
@Html.Raw(Model.Description)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Published)
</dt>
<dd>
@Html.DisplayFor(model => model.Published)
</dd>
<dt>
@Html.DisplayNameFor(model => model.ViewCount)
</dt>
<dd>
@Html.DisplayFor(model => model.ViewCount)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Author.AuthorName)
</dt>
<dd>
@Html.DisplayFor(model => model.Author.AuthorName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Category.CategoryName)
</dt>
<dd>
@Html.DisplayFor(model => model.Category.CategoryName)
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Sửa", "Edit", new { id = Model.BookID }) |
@Html.ActionLink("Quay trở lại danh sách", "Index")
</p>
Từ trang Index.cshtml. Sau đó, nhấp liên kết Xem trên giao diện Web để đi đến trang chỉnh sửa liên kết http://localhost:xxxx/Book/Edit/13 sẽ gọi ActionResult Details(int? id) trong controller BookController.cs Cho kết quả sau khi tùy chỉnh Details.cshtml.
2. Phương thức Details
Mở tập tin BookController.cs. Tiếp theo chúng ta sẽ xử lý trong Controller như sau, tìm đến hai phương thức Details(int? id):
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Book book = db.Books.Find(id);
if (book == null)
{
return HttpNotFound();
}
return View(book);
}
Phương thức Details(int? id) dùng để hiện thị thông tin sách bất kỳ theo chỉ số ID. Trong phương thức này, có thể kiểm tra xem giá trị đầu vào null hay không, nếu không thì dùng phương thức Find để tìm đối tượng Book có trong database theo ID hay không, nếu có thì hiển thị , không thì trả về phương thức HttpNotFound(), tức là không tìm thấy.
Ngoài lề 1 tý, kiểu int? có nghĩa là biến có thể là int hoặc là null.
int i1 = 1; //OK
int i2 = null; //not OK
int? i3 = 1; //OK
int? i4 = null; //OK, int? cho phép biến bằng null
Nếu một Book được tìm thấy theo chỉ số id, một thể hiện mô hình Book được đẩy sang View bằng dòng return View(book).