Search code examples
c#model-view-controllerdrop-down-menuselectedvalue

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


I would like to binding selectedvalue data from model to dropdownlist on view but still not working.

On dropdownlist still selected "Select Business Unit".

Any ideas do you have to suggestions me?

Thank you for your help in advance.

This is UserModel

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

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

This is usercontroller

* I had been to change the code from 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);
        }

This is userview

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

Solution

  • ViewBag variable should be ViewBag.businessUnit not ViewBag.businessUnits in the Modify action of usercontroller. Please see the usercontroller code below.

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