C# 从列表框中获取所选项目(使用Ajax填充)
我正在尝试从我的多选列表中获取所选项目。我真正想检索的只是一个选定的项目或选定的组,但组中的所有项目都足够了。我的列表一开始是空的,然后在使用Ajax选择不同的列表后填充 我的JQuery更改事件是这样的:C# 从列表框中获取所选项目(使用Ajax填充),c#,jquery,ajax,asp.net-mvc,bootstrap-multiselect,C#,Jquery,Ajax,Asp.net Mvc,Bootstrap Multiselect,我正在尝试从我的多选列表中获取所选项目。我真正想检索的只是一个选定的项目或选定的组,但组中的所有项目都足够了。我的列表一开始是空的,然后在使用Ajax选择不同的列表后填充 我的JQuery更改事件是这样的: $("#institutions").change(function() { $.ajax({ type: 'POST', url: '@Url.Action("FillGroup
$("#institutions").change(function() {
$.ajax({
type: 'POST',
url: '@Url.Action("FillGroupsAndFam")',
dataType: 'json',
data: { institutionId: Number($("#institutions").val()) },
success: function(model) {
$("#family-select").empty();
var $prevGroup, prevGroupName;
$.each(model.FamilyGroups, function() {
if (prevGroupName != this.Group.Name) {
$prevGroup = $(document.createElement("optgroup"))
.prop("label", this.Group.Name)
.appendTo("#family-select");
}
$(document.createElement("option"))
.val(this.Value)
.text(this.Text)
.appendTo("#family-select");
prevGroupName = this.Group.Name;
});
$("#family-select").multiselect('rebuild');
var famList = (from f in fam
let famGroup = new SelectListGroup {Name = f.FamilyGroupName}
from id in f.UserIds
select new SelectListItem
{
Text = UserManager.FindById(id).UserName, Value = id, Disabled = true, Group = famGroup
}).ToList();
@Html.LabelFor(m => m.CreateModel.FamilyGroups)
@Html.ListBoxFor(m => m.CreateModel.SelectedFamilyGroupId, Model.CreateModel.FamilyGroups, new {@class="form-control", @id="family-select"})
这将调用FillGroupsAndFam
,它像这样填充我的列表:
$("#institutions").change(function() {
$.ajax({
type: 'POST',
url: '@Url.Action("FillGroupsAndFam")',
dataType: 'json',
data: { institutionId: Number($("#institutions").val()) },
success: function(model) {
$("#family-select").empty();
var $prevGroup, prevGroupName;
$.each(model.FamilyGroups, function() {
if (prevGroupName != this.Group.Name) {
$prevGroup = $(document.createElement("optgroup"))
.prop("label", this.Group.Name)
.appendTo("#family-select");
}
$(document.createElement("option"))
.val(this.Value)
.text(this.Text)
.appendTo("#family-select");
prevGroupName = this.Group.Name;
});
$("#family-select").multiselect('rebuild');
var famList = (from f in fam
let famGroup = new SelectListGroup {Name = f.FamilyGroupName}
from id in f.UserIds
select new SelectListItem
{
Text = UserManager.FindById(id).UserName, Value = id, Disabled = true, Group = famGroup
}).ToList();
@Html.LabelFor(m => m.CreateModel.FamilyGroups)
@Html.ListBoxFor(m => m.CreateModel.SelectedFamilyGroupId, Model.CreateModel.FamilyGroups, new {@class="form-control", @id="family-select"})
在我看来,我是这样显示这个列表的:
$("#institutions").change(function() {
$.ajax({
type: 'POST',
url: '@Url.Action("FillGroupsAndFam")',
dataType: 'json',
data: { institutionId: Number($("#institutions").val()) },
success: function(model) {
$("#family-select").empty();
var $prevGroup, prevGroupName;
$.each(model.FamilyGroups, function() {
if (prevGroupName != this.Group.Name) {
$prevGroup = $(document.createElement("optgroup"))
.prop("label", this.Group.Name)
.appendTo("#family-select");
}
$(document.createElement("option"))
.val(this.Value)
.text(this.Text)
.appendTo("#family-select");
prevGroupName = this.Group.Name;
});
$("#family-select").multiselect('rebuild');
var famList = (from f in fam
let famGroup = new SelectListGroup {Name = f.FamilyGroupName}
from id in f.UserIds
select new SelectListItem
{
Text = UserManager.FindById(id).UserName, Value = id, Disabled = true, Group = famGroup
}).ToList();
@Html.LabelFor(m => m.CreateModel.FamilyGroups)
@Html.ListBoxFor(m => m.CreateModel.SelectedFamilyGroupId, Model.CreateModel.FamilyGroups, new {@class="form-control", @id="family-select"})
我的ViewModel
有以下功能:
[Display(Name="Family in Study")]
public IEnumerable<SelectListItem> FamilyGroups { get; set; }
public IEnumerable<SelectListItem> SelectedFamilyGroupId { get; set; }
[显示(Name=“研究中的家庭”)]
公共IEnumerable FamilyGroup{get;set;}
public IEnumerable SelectedFamilyGroupId{get;set;}
我通过将SelectedFamilyGroupId
类型更改为IEnumerable