Binding truyền dữ liệu vào DropDownList trong MVC Asp.net

Thảo luận trong 'Lập Trình Website MVC5 & MVC6' bắt đầu bởi admin, 23/7/18.

  1. admin
    Tham gia ngày:
    22/5/13
    Bài viết:
    4,905
    Đã được thích:
    1,199
    Điểm thành tích:
    113
    Giới tính:
    Nam
    Mình có đọc được 1 bài của bạn trên web cách Binding (truyền dữ liệu) vào DropDownList trong MVC Asp.net toàn tập với trọn bộ truyền dữ liệu, binding khá chi tiết như sau:

    Chuẩn bị:
    • Tạo 1 Controller Category có tên : CategoryController trong thư mục Controllers và 1 Model của Controller này có tên CategoryViewModel trong thư mục Models.
    CategoryController.cs
    Mã:
    public class CategoryController : Controller
    {
        // GET: Category
        public ActionResult Index()
        {
            var model = new CategoryViewModel();
    
            // using Model
            model.CategoryList = new SelectList(GetAllCategories(), "Id", "Name");
    
            // using ViewBag
            ViewBag.VbCategoryList = new SelectList(GetAllCategories(), "Id", "Name");
    
            return View(model);
        }
    
        public List<CategoryViewModel> GetAllCategories()
        {
            var categories = new List<CategoryViewModel>
                {
                    new CategoryViewModel {Id = 1, Name = "Code HTML MVC"},
                    new CategoryViewModel {Id = 2, Name = "Code ASP.NET MVC"},
                    new CategoryViewModel {Id = 3, Name = "Code PHP"},
                    new CategoryViewModel {Id = 4, Name = "Code Java"}
                    new CategoryViewModel {Id = 5, Name = "Code Ajax"}
                };
    
            return categories.ToList();
        }
    }
    
    CategoryViewModel.cs
    Mã:
    public class CategoryViewModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public SelectList CategoryList { get; set; }
    }
    Binding (Lấy dữ liệu vào DropDownList)

    Cách 1: Binding truyền dữ liệu vào DropDownList bằng cách sử dụng Model
    Mã:
    @Html.DropDownList("ddl1", Model.CategoryList, "--- Select ---")
    Cách 2: Binding truyền dữ liệu vào DropDownListFor bằng cách sử dụng Model
    Mã:
    @Html.DropDownListFor(m => m.Id, new SelectList(Model.CategoryList, "Value", "Text"), "--- Select ---")
    Cách 3: Binding truyền dữ liệu vào DropDownList bằng cách sử dụng ViewBag
    Mã:
    @Html.DropDownList("ddl2", ViewBag.VbCategoryList as SelectList, "--- Select ---")
    Cách 4: Binding truyền dữ liệu vào DropDownList bằng cách sử dụng ViewBag Căn Bản
    Mã:
    <select name="Category">
        @foreach (var item in ViewBag.VbCategoryList) {
            <option value="@item.Id">@item.Name</option>
        }
    </select>
    
    Cách 5: Binding truyền dữ liệu vào DropDownList bằng cách sử dụng Harcode
    Mã:
    @Html.DropDownList("ddl3", new List<SelectListItem>
    {
        new SelectListItem {Text = "Code HTML MVC", Value = "1"},
        new SelectListItem {Text = "Code ASP.NET MVC", Value = "2"},
        new SelectListItem {Text = "Code PHP", Value = "3"},
        new SelectListItem {Text = "Code Java", Value = "4"},
        new SelectListItem {Text = "Code Ajax", Value = "5"}  
    }, "--- Select ---")
    Cách 6: truyền dưới dạng SelectList ViewBag gọi dữ liệu từ Linq

    Trong Controller
    Mã:
    var modeldata = DbContext.DataCategory.Where(p=>p.Activate);
    var defaultvalue = 10;//giá trị mặc định truyền vào combobox.
    ViewBag.VBCategory = new SelectList(modeldata, "Id", "Name", defaultvalue);
    
    Trong View
    Mã:
    @Html.DropDownList("CategoryID", (SelectList)ViewBag.VBCategory, "Chọn", new { @class = "class-css-name" })
     
    Cảm ơn đã xem bài:

    Binding truyền dữ liệu vào DropDownList trong MVC Asp.net

    Chỉnh sửa cuối: 23/8/22


Chủ để tương tự : Binding truyền
Diễn đàn Tiêu đề Date
Lập Trình Website MVC5 & MVC6 Truyền giá trị mặc định cho @Html.TextBox bằng ViewBag MVC 6/8/18
Lập Trình Website MVC5 & MVC6 Truyền và lấy dữ liệu từ Controller sang View trong MVC asp.net 26/9/17
Lập Trình Website MVC5 & MVC6 Truyền và lấy dữ liệu từ View sang Controller trong MVC asp.net 26/9/17