🔒
Đăng ký để xem nội dung này
Tạo tài khoản miễn phí và đăng ký gói học để truy cập toàn bộ bài học, video và tài liệu độc quyền.
Đăng ký ngay — Miễn phí Đã có tài khoản? Đăng nhậpTruyền dữ liệu hiệu quả giữa controller, view, và model
TRUYỀN DỮ LIỆU HIỆU QUẢ GIỮA CONTROLLER, VIEW VÀ MODEL
🟢 PHẦN 1. TRUYỀN DỮ LIỆU TỪ CONTROLLER SANG VIEW
🎯 1.1 Tổng quan
| Cách truyền | Ưu điểm | Nhược điểm | Khi dùng |
|---|---|---|---|
| Model | Mạnh kiểu, IntelliSense | Phải định nghĩa class | Truyền dữ liệu chính |
| ViewBag | Nhanh, dynamic | Không kiểm tra kiểu | Dữ liệu phụ nhỏ |
| ViewData | Dictionary, giống ViewBag | Không gợi ý code | Dữ liệu phụ nhỏ |
| TempData | Qua redirect | Chỉ dùng 1 lần | Thông báo sau redirect |
🚀 1.2 Dùng Model (phổ biến nhất)
➕ Controller
public IActionResult Index()
{
var products = new List<string> { "Laptop", "Phone", "Tablet" };
return View(products);
}
➕ View
@model List<string>
<h2>Products</h2>
<ul>
@foreach (var item in Model)
{
<li>@item</li>
}
</ul>
✅ An toàn, gợi ý code, dễ bảo trì.
🔥 1.3 Dùng ViewBag
➕ Controller
public IActionResult About()
{
ViewBag.Message = "This is About Page.";
return View();
}
➕ View
<h2>@ViewBag.Message</h2>
✅ Nhanh, dynamic.
🔥 1.4 Dùng ViewData
public IActionResult Contact()
{
ViewData["Email"] = "support@example.com";
return View();
}
<p>Contact us: @ViewData["Email"]</p>
🔥 1.5 Dùng TempData
public IActionResult Save() { TempData["Notice"] = "Saved successfully!"; return RedirectToAction("Index"); } public IActionResult Index() { return View(); }
Trong View:
@if (TempData["Notice"] != null)
{
<div>@TempData["Notice"]</div>
}
✅ Giữ qua redirect.
🚀 1.6 Kết hợp ViewModel chuyên nghiệp
➕ Tạo Model
public class ProductViewModel
{
public int Id { get; set; }
public string Name { get; set; }
}
➕ Controller
public IActionResult Index()
{
var products = new List<ProductViewModel>
{
new ProductViewModel { Id = 1, Name = "Laptop" },
new ProductViewModel { Id = 2, Name = "Phone" }
};
return View(products);
}
➕ View
@model List<ProductViewModel>
<ul>
@foreach (var p in Model)
{
<li>@p.Id - @p.Name</li>
}
</ul>
🟠 PHẦN 2. TRUYỀN DỮ LIỆU TỪ VIEW LÊN CONTROLLER
🎯 2.1 Tổng quan
| Cách gửi dữ liệu | Ví dụ URL | Controller nhận |
|---|---|---|
| Route | /Product/Details/5 |
Details(int id) |
| Query | /Product/Search?key=abc |
Search(string key) |
🚀 2.2 Truyền qua Route Parameter
➕ Controller
public IActionResult Details(int id)
{
ViewBag.ProductId = id;
return View();
}
➕ View (thẻ <a>)
<a href="/Product/Details/5">Xem chi tiết 5</a>
➕ Hoặc ActionLink
@Html.ActionLink("Xem 5", "Details", "Product", new { id = 5 }, null)
🚀 2.3 Truyền qua Query String
➕ Controller
public IActionResult Search(string keyword)
{
ViewBag.Keyword = keyword;
return View();
}
➕ View
<a href="/Product/Search?keyword=laptop">Tìm Laptop</a>
🚀 2.4 Kết hợp Route & Query
public IActionResult Filter(string category, int page = 1)
{
ViewBag.Category = category;
ViewBag.Page = page;
return View();
}
Truy cập:
/Product/Filter?category=electronics&page=2
🚦 2.5 Custom route cho URL thân thiện
➕ Ví dụ Route Attribute
[Route("about-us")]
public IActionResult About()
{
return View();
}
➡ URL /about-us tự động gọi action About.
✅ Tóm tắt
| Từ đâu → Đến đâu | Dữ liệu gì | Khi nào |
|---|---|---|
| Controller → View | ViewModel, ViewBag, ViewData, TempData | Render trang |
| View → Controller | Route, Query String | Click link, route điều hướng |
Bài tập số 6: WebDaoTao
Yêu cầu:
-
Thực hành lại với Controller Khóa Học.
-
Tạo Controller Tin Tức và thực hành các cách truyền dữ liệu đã học, tương tự như phần Khóa Học.
- View TinHot: Truyền dạng list tin tức sang view bằng ViewBag
- View TinMoi: Truyền dạng list tin tức sang view bằng Model
Nộp bài:
-
Nén project đã làm và nộp file.