0

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>

1 Answer 1

1

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);
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer but it's still same result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.