我有3个多选下拉列表。当我从每个下拉列表中进行多项选择并提交时,我可以看到结果正在发布但没有传递给控制器。
我的观点
@model Facility.AdgarWeb.ViewModels.MailingConfigVM
@using (Html.BeginForm("New", "MailingConfig"))
{
@*Company List Dropdown*@
<div class="mailingConfiguration__modal__dropDown mailingConfiguration__modal__dropDown--companyList">
<h3 class="mailingConfiguration__modal__dropDown__title">Wybierz Budynki</h3>
<div class="dropdown companiesDropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="companiesButton" aria-haspopup="true" aria-expanded="false">
Select Building
</button>
@Html.LabelFor(m => m.allBuildings)
@Html.DropDownListFor(m => m.allBuildings, new MultiSelectList(Model.allBuildings, "Id", "Name"), new { @class="dropdown-menu", @multiple="true" })
</div>
</div>
@*Classification List Dropdown*@
<div class="mailingConfiguration__modal__dropDown mailingConfiguration__modal__dropDown--classificationList">
<h3 class="mailingConfiguration__modal__dropDown__title">Wybierz Klasyfikacje</h3>
<div class="dropdown classificationDropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="classificationButton" aria-haspopup="true" aria-expanded="false">
Select Classification
</button>
@Html.LabelFor(m => m.allServices)
@Html.DropDownListFor(m => m.allServices, new MultiSelectList(Model.allServices, "Id", "Name"), new { @class = "dropdown-menu", @multiple = "true" })
</div>
</div>
@*Users List Dropdown*@
<div class="mailingConfiguration__modal__dropDown mailingConfiguration__modal__dropDown--usersList">
<h3 class="mailingConfiguration__modal__dropDown__title">Wybierz Użytkowników</h3>
<div class="dropdown usersDropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="usersButton" aria-haspopup="true" aria-expanded="false">
Select Users
</button>
@Html.LabelFor(m => m.allUsers)
@Html.DropDownListFor(m => m.allUsers, new MultiSelectList(Model.allUsers, "Id", "UserName"), new { @class = "dropdown-menu", @multiple = "true" })
</div>
</div>
<input type="submit" class="btn btn-primary" name="New" value="Submit">
}
视图模型
public class MailingConfigVM
{
[Display(Name = "Buildings")]
public IEnumerable<Building> allBuildings { get; set; }
[Display(Name = "Services")]
public IEnumerable<Facility.Model.Service> allServices { get; set; }
[Display(Name = "Users")]
public IEnumerable<User> allUsers { get; set; }
}
控制器
public ActionResult Create()
{
if (!SessionControlService.CheckIsLoginStillTrue(_loginsService, HttpContext))
return RedirectToAction("Login", "Account");
MailingConfigVM vm = new MailingConfigVM();
vm.allBuildings = _buildingService.GetBuildings();
vm.allServices = _serviceService.GetServices();
vm.allUsers = _userService.GetUsers().Where(user => user.Roles.Any(r => r.Id <= 4));
var buildings = _buildingService.GetBuildings().ToList();
return View(vm);
}
[Authorize(Roles = "Admin")]
[HttpPost]
public ActionResult New(MailingConfigVM mailingConfigVM)
{
if (!SessionControlService.CheckIsLoginStillTrue(_loginsService, HttpContext))
return RedirectToAction("Login", "Account");
if (!User.IsInAnyRoles("Admin"))
return RedirectToAction("Index");
return RedirectToAction("Create", "MailingConfig");
}
创建控制器使用数据填充表单以供选择,新控制器为httppost。我希望将所选项目传递给此控制器,但在POST后,结果为&#34; COUNT = 0&#34;在每个列表上。
我将不胜感激。
答案 0 :(得分:1)
您无法将this.setState
绑定到复杂对象的集合,这就是您的属性。 <select multiple>
发回一系列简单值。
您的模型需要3个附加属性(请注意,这假设您的<select multiple>
,Id
和Building
模型的Service
属性属于User
类型<) / p>
int
此外,您应该在控制器中创建集合属性public IEnumerable<int> SelectedBuildings { get; set; }
public IEnumerable<int> SelectedServices { get; set; }
public IEnumerable<int> SelectedUsers { get; set; }
并使用IEnumerable<SelectListItem>
,而不是创建选项的视图,例如
new SelectList(...)
并在控制器中
public IEnumerable<SelectListItem> AllBuildings { get; set; }
....
最后,为了正确绑定,您必须使用vm.AllBuildings = new SelectList(_buildingService.GetBuildings(), "Id", "Name");
,而不是Why does the DropDownListFor lose the multiple selection after Submit but the ListBoxFor doesn't?
ListBoxFor()
您的观看代码现在将
DropDownListFor()