我将此DropDownListFor添加到我的(部分)视图 _CreateUser.cshtml 中:
<div class="modal fade bs-example-modal-lg" id="createUserModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Create A User</h4>
</div>
@using (Html.BeginForm("Create", "Users", FormMethod.Post))
{
<div class="modal-body">
@Html.LabelFor(model => model.Departments)
<div>
@Html.DropDownListFor(
model => model.SelectedDepartmentIds,
new SelectList(Model.DepartmentSelectList, "Value", "Text"),
new {@class = "multiselect departments", multiple = "multiple"})
</div>
...
以上部分视图在主视图 Index.cshtml 中声明,并在点击&#39;创建&#39;时显示为模式弹出窗口。按钮
@Html.ActionLink("Create user", "Create", "Users", new { @class = "btn btn-primary", @href = "#createUserModal", @data_toggle="modal" })
...
@Html.Partial("_CreateUser", new UserViewModel()
{ //Populating SelectList required for loading dropdownlist
DepartmentSelectList = modelFactory != null ?
modelFactory.Departments.Select(dept => new SelectListItem{Value = dept.Id.ToString(),Text = dept.Name}).ToList()
: null
})
UserViewModel.cs
public IEnumerable<String> Departments { get; set; }
//Used purely for binding DropDownList
public IEnumerable<SelectListItem> DepartmentSelectList { get; set; }
public string SelectedDepartmentIds { get; set; }
//Ideally, I want to populate selected items in this list
public IEnumerable<DepartmentModel> DepartmentModels { get; set; }
UserController.cs
[HttpPost]
public ActionResult Create(UserViewModel postedModel)
{
if (ModelState.IsValid)
{
//Incorrect - returns only first selected Id
string selectedIds = postedModel.SelectedDepartmentIds;
//Correct - returns correct selected Ids
string selectedIds1 = ModelState["SelectedDepartmentId"].Value.AttemptedValue;
...
...
两个问题:
如何从DropDownList中检索所有选定的项目,作为模型属性的一部分&#34; SelectedDepartmentId&#34;?我是否需要在运行时使用jQuery更新ModalProperty?
我可以设置我的视图以便绑定Modal属性&#34; DepartmentModels&#34;使用DropDownList - 它将帮助我检索所选项目的完整对象?
目前,如果我尝试这样做,我会收到此错误:
{&#34;参数转换类型&#39; System.String&#39;键入&#39; ... DepartmentModel&#39;失败,因为没有类型转换器可以在这些类型之间进行转换。&#34;}
谢谢!
答案 0 :(得分:0)
在查看各种Google搜索后得到了解决:-)
下面是正确加载和返回所选对象的代码 -
<强>型号:强>
public class UserGroupViewModel
{
public IEnumerable<SelectListItem> GroupSelectList
{
get
{
List<Group> groups = new List<Group>
{
new Group() {Id = 1, Name = "AA"},
new Group() {Id = 2, Name = "BB"},
new Group() {Id = 3, Name = "CC"}
};
IEnumerable<SelectListItem> groupsSelectList = groups.Select(group => new SelectListItem { Text = group.Name, Value = group.Id.ToString(), Selected = group.Id == 2}).ToList();
return groupsSelectList;
}
}
[Required]
[Display(Name = "Group")]
public int[] SelectedGroupIds { get; set; }
}
查看:强>
<div class="form-group">
@Html.LabelFor(model => model.SelectedGroupIds, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.SelectedGroupIds,
Model.GroupSelectList,
new { @class = "multiselect", multiple = "multiple" })
@Html.ValidationMessageFor(model => model.SelectedGroupIds)
</div>
</div>
要在表单加载时将某些项目显示为“已选择”,只需将SelectListItem的“Selected”属性设置为true即可。
HTH。