如何在编辑模式 MVC 中将数据从数据库绑定到 DropDownList?

How to binding data from database to DropDownList in Edit Mode MVC?

我想将模型中的选定值数据绑定到视图中的下拉列表,但仍然无法正常工作。

在下拉列表中仍处于选中状态 "Select Business Unit"。

你有什么想法可以给我建议吗?

提前感谢您的帮助。

这是用户模型

public class UserModel
    {

        //public static ClaimsIdentity Identity { get; set; }
        [Key]
        public Int64 UserId { get; set; }
        [Required]
        public string UserCode { get; set; }
        [Required]
        public string UserName { get; set; }
        [Required]
        public string UserEmail { get; set; }
        [Required]
        public string UserPassword { get; set; }
        [Required]
        public string UserRole { get; set; }
        [Required]
        public string UserStatus { get; set; }
        [Required]
        public DateTime LastLogin_Date { get; set; }
        [Required]
        public string Create_By { get; set; }
        [Required]
        public DateTime Create_Date { get; set; }
        [Required]
        public string Update_By { get; set; }
        [Required]
        public DateTime Update_Date { get; set; }

        public string BU_Code { get; set; }
        public virtual BusinessUnitModel BusinessUnits { get; set; }
    }

这是 BusinessUnitModel

public class BusinessUnitModel
    {
        public BusinessUnitModel()
        {
            this.Users = new HashSet<UserModel>();
            this.Departments = new HashSet<DepartmentModel>();
        }

        [Key]
        public string BU_Code { get; set; }
        [Required]
        public string BU_Name { get; set; }
        [Required]
        public string BU_Prefix { get; set; }
        [Required]
        public string BU_Type { get; set; }
        [Required]
        public string BU_Status { get; set; }
        [Required]
        public string Create_By { get; set; }
        [Required]
        public DateTime Create_Date { get; set; }
        [Required]
        public string Update_By { get; set; }
        [Required]
        public DateTime Update_Date { get; set; }

        public virtual ICollection<UserModel> Users { get; set; }
        public virtual ICollection<DepartmentModel> Departments { get; set; }
        public virtual ICollection<StockModel> Stocks { get; set; }
    }

这是用户控制器

* 我一直在更改代码 ViewBag.businessUnits ==> ViewBag.businessUnit *

public IActionResult Modify(Int64 id)
        {
            UserModel users = _user.GetAll(id);
            ViewBag.businessUnit = new SelectList(_businessUnit.GetAll("A").Select(x => new
            {
                Value = x.BU_Code,
                Text = x.BU_Name
            }), "Value", "Text", users.BU_Code);

            return View(users);
        }

这是用户视图

<div class="col">
@Html.LabelFor(model => model.BusinessUnits, htmlAttributes: new { @class = "control-label" })
@Html.DropDownListFor(model => model.BusinessUnits, (IEnumerable<SelectListItem>)ViewBag.businessUnit, "Select Business Unit", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.BusinessUnits)
</div>

ViewBag 变量应该是 ViewBag.businessUnit 而不是 usercontroller 的修改操作中的 ViewBag.businessUnits。请参阅下面的用户控制器代码。

public IActionResult Modify(Int64 id)
        {
            UserModel users = _user.GetAll(id);
            ViewBag.businessUnit = new SelectList(_businessUnit.GetAll("A").Select(x => new
            {
                Value = x.BU_Code,
                Text = x.BU_Name
            }), "Value", "Text", users.BU_Code);

            return View(users);
        }