-->

Học lập trình ASP.NET MVC cơ bản qua bài tập cụ thể (Phần 2)

Ở phần trước chúng ta đã cũng làm những bài tập cơ bản nhất, và hôm chúng ta cũng nâng cao hơn với nhiều dạng bài mới hơn để cùng nhau luyện tập nhé. Bài viết này chú tâm vào các kiểu truy vấn và các nghiệp vụ xung quanh nó.

hoc-lap-trinh-aspnet-mvc-co-ban-qua-bai-tap-cu-the

Đề bài

Và các tasks chúng ta cùng nghiên cứu trong ngày hôm nay như sau:
Tạo thực thể Xe(Id, TenXe, HangXe, NgaySanXuat, NgayBanXe, ChuXe, SoGhe) trong cơ sở dữ liệu, kết nối đến project ASP.NET MVC và thực hiện:

0. Thực thi CRUD với thực thể Customer.
1. In ra danh sách xe có số ghế bằng 4 và 6
2. In ra danh sách xe thuộc hãng Audi
3. In ra danh sách xe thuộc hãng Toyota và có ngày sản xuất trước ngày 1/1/2015
4. In ra danh sách xe có ngày bán xe sau ngày sản xuất 30 ngày
5. In ra danh sách xe có số ghế lớn hơn 3 và nhỏ hơn 10

Có lẽ bạn thích?

6. In ra danh sách xe giảm dần theo Id
7. In ra danh sách xe tăng dần theo hãng xe và có chủ xe tên là Phuong
8. In ra danh sách xe có tên chủ xe bắt đầu bẵng chữ H và hãng xe Toyota

Tạo database

Trước tiên bạn cần tạo một database giống như thực thể ở đề bài và thêm dữ liệu mẫu:
hoc-lap-trinh-aspnet-mvc-co-ban-qua-bai-tap-cu-the

Tạo project ASP.NET MVC

Phần này chúng ta cũng thực hành khá nhiều, nên mình sẽ lướt qua. Các bạn xem lại hướng dẫn ở bài 1 tại link dưới đây:


Tuy nhiên, project mình tạo theo cấu trúc sau:

  • VehicleManagement.Data (Class Library (.NET Framework)) 
  • VehicleManagement.WebApp (ASP.NET Web Application (.NET Framework))

Tạo CRUD cho thực thể

Trong VehicleManagement.WebApp tạo controller mới (empty) tên là VehicleController.cs và thêm lần lượt code các phương thức sau và view sau:

VehicleController.cs

Tất cả các phương thức thực thi CRUD cho thực thể Xe nằm hết trong Controller sau:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using VehicleManagement.Data.Entities;
using System.Globalization;


namespace VehicleManagement.WebApp.Controllers
{
    public class VehicleController : Controller
    {
        private DataDbContext db = new DataDbContext();

        // GET: Vehicle
        public ActionResult Index()
        {
             return View();

        }

        // GET: Vehicle/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Xe xe = db.Xes.Find(id);
            if (xe == null)
            {
                return HttpNotFound();
            }
            return View(xe);
        }

        // GET: Vehicle/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Vehicle/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,TenXe,HangXe,NgaySanXuat,NgayBanXe,ChuXe,SoGhe")] Xe xe)
        {
            if (ModelState.IsValid)
            {
                db.Xes.Add(xe);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(xe);
        }

        // GET: Vehicle/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Xe xe = db.Xes.Find(id);
            if (xe == null)
            {
                return HttpNotFound();
            }
            return View(xe);
        }

        // POST: Vehicle/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,TenXe,HangXe,NgaySanXuat,NgayBanXe,ChuXe,SoGhe")] Xe xe)
        {
            if (ModelState.IsValid)
            {
                db.Entry(xe).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(xe);
        }

        // GET: Vehicle/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Xe xe = db.Xes.Find(id);
            if (xe == null)
            {
                return HttpNotFound();
            }
            return View(xe);
        }

        // POST: Vehicle/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Xe xe = db.Xes.Find(id);
            db.Xes.Remove(xe);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}



Tương ứng với các view sau đây trong Views/Vehicle:

Index.cshtml - Hiển thị danh sách

@model IEnumerable<VehicleManagement.Data.Entities.Xe>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Id)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.TenXe)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.HangXe)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.NgaySanXuat)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.NgayBanXe)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ChuXe)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.SoGhe)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Id)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.TenXe)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.HangXe)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.NgaySanXuat)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.NgayBanXe)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ChuXe)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.SoGhe)
            </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>


Details.cshtml - Trang hiển thị chi tiết

@model VehicleManagement.Data.Entities.Xe

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

<div>
    <h4>Xe</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.TenXe)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.TenXe)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.HangXe)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.HangXe)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.NgaySanXuat)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.NgaySanXuat)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.NgayBanXe)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.NgayBanXe)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.ChuXe)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.ChuXe)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.SoGhe)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.SoGhe)
        </dd>

    </dl>
</div>
<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.Id }) |
    @Html.ActionLink("Back to List", "Index")
</p>

Create.cshtml - Trang thêm mới:

@model VehicleManagement.Data.Entities.Xe

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>Xe</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.TenXe, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.TenXe, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.TenXe, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.HangXe, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.HangXe, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.HangXe, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NgaySanXuat, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.NgaySanXuat, new { htmlAttributes = new { @class = "form-control", @type = "date" } })
                @Html.ValidationMessageFor(model => model.NgaySanXuat, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NgayBanXe, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.NgayBanXe, new { htmlAttributes = new { @class = "form-control", @type = "date" } })
                @Html.ValidationMessageFor(model => model.NgayBanXe, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ChuXe, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ChuXe, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ChuXe, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.SoGhe, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.SoGhe, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.SoGhe, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Edit.cshtml - Trang chỉnh sửa


@model VehicleManagement.Data.Entities.Xe

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>Xe</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.Id)

        <div class="form-group">
            @Html.LabelFor(model => model.TenXe, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.TenXe, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.TenXe, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.HangXe, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.HangXe, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.HangXe, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NgaySanXuat, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.NgaySanXuat, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NgaySanXuat, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NgayBanXe, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.NgayBanXe, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NgayBanXe, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ChuXe, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ChuXe, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ChuXe, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.SoGhe, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.SoGhe, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.SoGhe, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}



Delete.cshtml - Trang xem lại dữ liệu trước khi xóa:


@model VehicleManagement.Data.Entities.Xe

@{
    ViewBag.Title = "Delete";
}

<h2>Delete</h2>

<h3>Are you sure you want to delete this?</h3>
<div>
    <h4>Xe</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.TenXe)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.TenXe)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.HangXe)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.HangXe)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.NgaySanXuat)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.NgaySanXuat)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.NgayBanXe)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.NgayBanXe)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.ChuXe)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.ChuXe)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.SoGhe)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.SoGhe)
        </dd>

    </dl>

    @using (Html.BeginForm()) {
        @Html.AntiForgeryToken()

        <div class="form-actions no-color">
            <input type="submit" value="Delete" class="btn btn-default" /> |
            @Html.ActionLink("Back to List", "Index")
        </div>
    }
</div>



Hoàn tất! Bây giờ hãy Build lại solution và chạy project để xem kết quả nhé:

 

In ra danh sách xe có số ghế bằng 4 và 6

Để lấy ra danh sách xe có số ghế bằng 4 và 6, ý tưởng là ta sẽ dùng truy vấn Linq kết hợp với mệnh đề where để lọc dữ liệu:

db.Xes.Where(s => s.SoGhe == 6 || s.SoGhe == 4).ToList();


Lưu ý: Hầu hết các bài toán đều đưa ra View giống nhau nên chúng ta hãy đi qua lần lượt cách truy vấn Linq rồi hiển thị View sau:

Where là method được dùng phổ biến nhất trong LINQ, Nó có thể dùng để lọc các phần tử trong một collection dựa trên các điều kiện nhất định.

In ra danh sách xe thuộc hãng Audi 

Cách đơn giản nhất để làm bài này là ta đi so sánh chuỗi, nhưng lỡ như người dùng không nhập Audi mà người ta nhập audi, AUDI,.. thì ta xử lý thế nào? Đơn giản là định dạng chữ thường hết rồi so sánh:

db.Xes.Where(s => s.HangXe.ToLower() == "audi").ToList();

Ta dùng ToLower() để định dạng chuỗi thành chữ in thường nhé.

In ra danh sách xe thuộc hãng Toyota và có ngày sản xuất trước ngày 1/1/2015

Bài này có rất nhiều cách để xử lý, nhưng mình nâng cao hơn một chút khi dùng kĩ thuật sau:

DateTime nsxAfter = DateTime.Now.Subtract(new TimeSpan(2015, 1, 1, 0, 0));

db.Xes.Where(s => s.HangXe.ToLower() == "toyota" && s.NgaySanXuat < nsxAfter).ToList();

Như code trên, mình dùng phương thức Subtract() để tính hiệu (trừ đi) 2 ngày tháng là DateTime.Now và ngày 1/1/2015. Vì Subtract() sẽ trả về một TimeSpan nên ngày 1/1/2015 cũng cần cùng kiểu dữ liệu.
Và cuối cùng là ta dùng truy vấn Linq như bình thường.

Một đối tượng TimeSpan biểu thị một khoảng thời gian (khoảng thời gian hoặc thời gian đã trôi qua) được đo bằng số ngày dương hoặc âm của ngày, giờ, phút, giây và phân số của giây. Cấu trúc TimeSpan cũng có thể được sử dụng để biểu thị thời gian trong ngày, nhưng chỉ khi thời gian không liên quan đến một ngày cụ thể.
 

In ra danh sách xe có ngày bán xe sau ngày sản xuất 30 ngày

Và bài này ta cũng sử dụng TimeSpan với code:

db.Xes.ToList().Where(s => (s.NgayBanXe.Value - s.NgaySanXuat.Value) < TimeSpan.FromDays(30));

TimeSpan cung cấp các phương thức FromDays, FromHours, FromMinutes, FromSeconds, and FromMilliseconds để tạo nên một đối tượng TimeSpan với ngày, giờ, phút, giây và mili giây tương ứng.

In ra danh sách xe có số ghế lớn hơn 3 và nhỏ hơn 10

Bài này ta so sánh rất đơn giản với so sánh với một số cụ thể:

db.Xes.Where(s => s.SoGhe > 3 && s.SoGhe < 10).ToList();


In ra danh sách xe giảm dần theo Id

Việc sắp xếp giảm dần ta dùng .OrderByDescending() đế sắp xếp, ngược lại để sắp xếp tăng dần ta dùng .OrderBy()

db.Xes.OrderByDescending(s => s.Id).ToList();


In ra danh sách xe tăng dần theo hãng xe và có chủ xe tên là Phuong

Bài này khá là hay, vì chúng ta cần đến một kĩ thuật đó là tách chuỗi ra, cụ thể với tên chủ xe là Nguyễn Văn Phương, thì bạn cần lấy tên Phương sau đó ta mới tiến hành truy vấn, cụ thể code như sau: 

List < Xe > listXe = db.Xes.ToList();
List < Xe > listXeAfter = new List < Xe > ();

foreach(var xe in listXe) {
  String[] FullName = xe.ChuXe.Split(" ".ToCharArray());
  String LastName = FullName[FullName.Length - 1];
  LastName = " " + LastName;

  if (LastName.ToLower().Contains(" h")) {
    listXeAfter.Add(xe);
  }

}

return View(listXeAfter);

In ra danh sách xe có tên chủ xe bắt đầu bằng chữ H và hãng xe Toyota

Và chúng ta sẽ đi đến ví dụ cuối cùng sau đây với một vài thao tác rất đơn giản với code sau:

return View(db.Xes.Where(s => s.ChuXe.ToLower().StartsWith("h") && s.HangXe.ToLower() == "toyota").ToList());


Lời kết

Và sau đây là code của method Index trong controller và view của Index.cshtml được cập nhật như sau:
Index()

public ActionResult Index(string listType) {
  ViewBag.Task1 = String.IsNullOrEmpty(listType) ? "task1": "";
  ViewBag.Task2 = String.IsNullOrEmpty(listType) ? "task2": "";
  ViewBag.Task3 = String.IsNullOrEmpty(listType) ? "task3": "";
  ViewBag.Task4 = String.IsNullOrEmpty(listType) ? "task4": "";
  ViewBag.Task5 = String.IsNullOrEmpty(listType) ? "task5": "";
  ViewBag.Task6 = String.IsNullOrEmpty(listType) ? "task6": "";
  ViewBag.Task7 = String.IsNullOrEmpty(listType) ? "task7": "";
  ViewBag.Task8 = String.IsNullOrEmpty(listType) ? "task8": "";

  switch (listType) {
  case "task1":
    return View(db.Xes.Where(s =>s.SoGhe == 6 || s.SoGhe == 4).ToList());
  case "task2":
    return View(db.Xes.Where(s =>s.HangXe.ToLower() == "audi").ToList());
  case "task3":
    DateTime nsxAfter = DateTime.Now.Subtract(new TimeSpan(2015, 1, 1, 0, 0));

    return View(db.Xes.Where(s =>s.HangXe.ToLower() == "toyota" && s.NgaySanXuat < nsxAfter).ToList());
  case "task4":
    return View(db.Xes.ToList().Where(s =>(s.NgayBanXe.Value - s.NgaySanXuat.Value) < TimeSpan.FromDays(30)));
  case "task5":
    return View(db.Xes.Where(s =>s.SoGhe > 3 && s.SoGhe < 10).ToList());
  case "task6":
    return View(db.Xes.OrderByDescending(s =>s.Id).ToList());
  case "task7":
    List < Xe > listXe = db.Xes.ToList();
    List < Xe > listXeAfter = new List < Xe > ();

    foreach(var xe in listXe) {
      String[] FullName = xe.ChuXe.Split(" ".ToCharArray());
      String LastName = FullName[FullName.Length - 1];
      LastName = " " + LastName;

      if (LastName.ToLower().Contains(" h")) {
        listXeAfter.Add(xe);
      }

    }

    return View(listXeAfter);
  case "task8":
    return View(db.Xes.Where(s =>s.ChuXe.ToLower().StartsWith("h") && s.HangXe.ToLower() == "toyota").ToList());
  default:
    return View(db.Xes.ToList());

  }

}


Index.cshtml

@model IEnumerable<VehicleManagement.Data.Entities.Xe>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
@Html.ActionLink("Task 1", "Index", new { listType = ViewBag.Task1 }) |
@Html.ActionLink("Task 2", "Index", new { listType = ViewBag.Task2 }) |
@Html.ActionLink("Task 3", "Index", new { listType = ViewBag.Task3 }) |
@Html.ActionLink("Task 4", "Index", new { listType = ViewBag.Task4 }) |
@Html.ActionLink("Task 5", "Index", new { listType = ViewBag.Task5 }) |
@Html.ActionLink("Task 6", "Index", new { listType = ViewBag.Task6 }) |
@Html.ActionLink("Task 7", "Index", new { listType = ViewBag.Task7 }) |
@Html.ActionLink("Task 8", "Index", new { listType = ViewBag.Task8 })
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Id)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.TenXe)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.HangXe)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.NgaySanXuat)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.NgayBanXe)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ChuXe)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.SoGhe)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Id)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.TenXe)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.HangXe)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.NgaySanXuat)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.NgayBanXe)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ChuXe)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.SoGhe)
            </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>


Bài tập này cơ bản vận dụng được các chức năng khi thao tác với data table, có thể dùng khi làm project lớn. Mọi thắc mắc comment ở dưới, hoặc liên hệ mình.

Mong bài viết hữu ích với bạn, chúc bạn thành công.

By Hiếu Quốc

Post a Comment