【问题标题】:ASP.NET Model Binding for Multiple Dropdownlist多个下拉列表的 ASP.NET 模型绑定
【发布时间】:2021-04-27 09:53:10
【问题描述】:

我想用 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_PIC(即List&lt;String&gt;)设置为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-mvc asp.net-web-api


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-02-25
      • 1970-01-01
      • 1970-01-01
      • 2012-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多