ViewModel được dùng để làm gì trong ASP.NET MVC?
ViewModel được dùng để làm gì trong ASP.NET MVC?
ViewModel là lớp chứa đúng dữ liệu một View cần (có thể gồm nhiều Model, danh sách chọn, cờ UI, thuộc tính tính toán), thay vì truyền trực tiếp entity/ domain model.
public class CheckoutViewModel
{
public IReadOnlyList<CartItemVm> Items { get; set; } = [];
public decimal Total => Items.Sum(i => i.Price * i.Qty);
[Required, StringLength(200)] public string ShippingAddress { get; set; } = "";
public SelectList PaymentMethods { get; set; } = default!; // dữ liệu cho dropdown
public bool CanUsePoints { get; set; }
}
public IActionResult Checkout() => View(new CheckoutViewModel { Items = ..., PaymentMethods = new SelectList(...) });
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.