phương pháp MVC để ràng buộc danh sách tùy chỉnh để DropDownList và chọn mục tự động nếu bạn cần biết thêm chi tiết, bình luận dưới đây
Tạo Phần
@{
List<SelectListItem> list = new List<SelectListItem>();
list.Add(new SelectListItem { Text = "SALE", Value = "SAL" });
list.Add(new SelectListItem { Text = "PURCHASE", Value = "PUR" });
}
<div class="form-group">
@Html.LabelFor(model => model.SaleOrPurchase, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.SaleOrPurchase, list, "-- Select Status --", new {@class= "form-control" })
@Html.ValidationMessageFor(model => model.SaleOrPurchase, "", new { @class = "text-danger" })
</div>
</div>
EDIT Phần
List<SelectListItem> list = new List<SelectListItem>();
list.Add(new SelectListItem { Text = "SALE", Value = "SAL" });
list.Add(new SelectListItem { Text = "PURCHASE", Value = "PUR" });
IEnumerable<SelectListItem> myCollection = list.AsEnumerable();
ViewBag.SaleOrPurchase = new SelectList(myCollection, "Value", "Text", transactionTbl.SaleOrPurchase.ToString().Trim());
Nguồn
2016-03-23 17:48:44
Cảm ơn, tôi đã làm bên dưới mã để đặt trong giao diện của tôi cho từng danh sách. –
@ { ViewData ["Sectors"] = new SelectList (Model.SectorList, "MainCategoryId", "CatName", "37"); } @ Html.DropDownList ("SectorId" + i, (IEnumerable) Chế độ xem ["Ngành"], "- Chọn -", mới {@class = "inputbox-small"}) –
Và? ! Tôi hy vọng bạn không đặt tất cả chúng thành 'true'. Điều đó sẽ đánh bại mục đích thiết lập thuộc tính 'Đã chọn' ... – xandercoded