c# - 多个下拉列表的 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_PIC
中List<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" })
解决方案
只是从 DropDownListFor 更改为 ListBoxFor 解决了我的问题。我的代码中没有发生其他更改
推荐阅读
- powershell - 如何仅从 PowerCLI 获取 currentVersion?
- javascript - Chrome DevTools 中的“暂停脚本执行”未按预期工作
- azure - Azure Blockchain Workbench REST API 返回 204 无内容
- ruby-on-rails - 未定义的方法“simple_form_for”
- php - ERR_TOO_MANY_REDIRECTS laravel 管理/登录页面
- amazon-web-services - EC2 实例状态
- windows-10 - 如何在 Windows 10 计算机用户上以编程方式安装 Windows 10 Text To Speech Voices?
- dart - Flutter:构造函数中List参数的默认分配
- javascript - 如何使用 jsPdf 设置修剪框?
- android - Android Studio 说“ADB.exe 已过时并且存在严重的性能问题”,尽管 SDK 工具说一切都是最新的