简体   繁体   中英

ASP.NET Model Binding for Multiple Dropdownlist

i want to set multiple dropdownlist with value from db.

Here the code:

VIEW

 @Html.DropDownListFor(model => model.rcf_data.lnkt_PIC, Model.Emp_list.EmployeList, new { @class = "show-tick form-control", id = "EmployeeList9", multiple = "multiple" })

CONTROLLER

In the controller, i use MergeModel

    public ActionResult Edit(String rcf_id)
            {
                //List<RCFHistory> list_history_status = 
    
                MergeModel model = new MergeModel();
                model.rcf_history = new List<RCFHistory>();
                model.rcf_history = GetRCFHistory(rcf_id);
                model.rcf_data = list_rcf.Where(r => r.lnkt_name == rcf_id).FirstOrDefault();
                model.Emp_list = new ListEmployeeMaster();
                model.Emp_list.EmployeList = new SelectList(GetAllEmployee(), "emp_id", "emp_name");
                return View(model);
            }

private IEnumerable<EmployeeNameModel> GetAllEmployee()
        {
            list_emp = new List<EmployeeNameModel>();
            string apiUrl = "http://localhost:6161/Service1.svc";

            HttpClient client = new HttpClient();
            HttpResponseMessage response = client.GetAsync(apiUrl + string.Format("/GetAllEmployee")).Result;
            if (response.IsSuccessStatusCode)
            {
                list_emp = (new JavaScriptSerializer()).Deserialize<List<EmployeeNameModel>>(response.Content.ReadAsStringAsync().Result);
            }

            return list_emp;
        }

I want to set value from model.rcf_data.lnkt_PIC which is List<String> into DropDownListFor in the view. But it keep showing empty field.

For single DropDownListFor, this line work for me, and it's showing value that i get from DB:

 @Html.DropDownListFor(model => model.rcf_data.lnkt_2ndapprovalid, Model.Emp_list.EmployeList, Model.rcf_data.lnkt_2ndapprovalid, new { id = "EmployeeList2", @class = "show-tick form-control" })

Just changing from DropDownListFor to ListBoxFor solved my problem. No other changes happen within my code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM