首页 > 解决方案 > 多个下拉列表的 ASP.NET 模型绑定

问题描述

我想使用来自 db 的值设置多个下拉列表。

这里的代码:

看法

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

控制器

在控制器中,我使用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;
        }

我想设置视图model.rcf_data.lnkt_PICList<String>的值DropDownListFor。但它一直显示空白字段。

对于单个 DropDownListFor,这条线对我有用,它显示了我从 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" })

标签: c#asp.net-mvcasp.net-web-api

解决方案


只是从 DropDownListFor 更改为 ListBoxFor 解决了我的问题。我的代码中没有发生其他更改


推荐阅读