【问题标题】:How to binding data from database to DropDownList in Edit Mode MVC?如何在编辑模式 MVC 中将数据从数据库绑定到 DropDownList?
【发布时间】:2019-04-12 03:37:26
【问题描述】:

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

在下拉列表中仍选择“选择业务单位”。

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

提前感谢您的帮助。

这是用户模型

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; }
    }

这是业务单元模型

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>

【问题讨论】:

    标签: c# model-view-controller drop-down-menu selectedvalue


    【解决方案1】:

    ViewBag 变量应该是 ViewBag.businessUnit 而不是用户控制器的 Modify 操作中的 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);
            }
    

    【讨论】:

    • 谢谢你的回答,但结果还是一样。
    猜你喜欢
    • 1970-01-01
    • 2021-03-06
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2010-09-26
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多