我想要做的是首先我需要显示我需要添加到组中的成员列表。然后,用户将单击用户选择的每行成员的每个复选框。我的代码已经运行了。问题是,当我应用jquery.dataTable(也完全呈现)时,当某些成员被隐藏(通过下一页或搜索)时,它似乎不会返回所有必要的数据。当我点击show 100,显示dataTable的每个隐藏数据时,一切正常。
我的ViewModel
public class ChooseMembersEditorViewModel
{
public bool Selected { get; set; }
public string Id { get; set; }
public string Name { get; set; }
public string Sex { get; set; }
public string Age { get; set; }
public ChooseMembersEditorViewModel()
{
this.Selected = false;
this.Id = "";
}
}
位于另一个ViewModel
中public class AddMemberViewModel
{
public List<Group> Group{ get; set; }
public List<ChooseMembersEditorViewModel> LGMembers { get; set; }
public AddLifegroupMemberViewModel()
{
this.Group= new List<Group>();
this.LGMembers = new List<ChooseMembersEditorViewModel>();
}
public IEnumerable<string> getSelectedIds()
{
var getSelected = (from members in this.LGMembers
where members.Selected
select members.Id); //returns error here saying value cannot be null
System.Diagnostics.Debug.WriteLine(string.Format("Count {0}", getSelected.Count()));
return getSelected;
}
}
当我检查其中一个值并点击Add Member时,我从getSelectedIds Linq中获取的值不能为null
提前谢谢!
修改
这是我列出所有成员的方式。 SubmitSelectedMembers基本上会获取其ID已被检查的所有成员,并将其添加到数据库中。
[Authorize(Roles = "Admin")]
public ActionResult AddLifegroupMembers(int id)
{
var model = new AddLifegroupMemberViewModel();
Lifegroup lifegroup = db.Lifegroup.Find(id);
int leaderAge = GetAge(lifegroup.LGLeader.User.Birthday);
ViewBag.Age = leaderAge;
System.Diagnostics.Debug.WriteLine(string.Format("{0}", lifegroup.LGName.ToString()));
var nonMembers = from u in db.Users
where !(from lgm in db.LGMember
select lgm.UserID).Contains(u.Id) //should not be part of any Lifegroup
&&
!(from l in db.Lifegroup
where l.LifegroupID.Equals(id)
select l.LGLeader.UserID).Contains(u.Id) //should not include leader of said group
select u;
System.Diagnostics.Debug.WriteLine(string.Format("Number of LG: {0}", model.Lifegroup.Count()));
foreach (var user in nonMembers)
{
int age = GetAge(user.Birthday);
var editorViewModel = new ChooseMembersEditorViewModel()
{
Id = user.Id,
Name = string.Format("{0} {1}", user.FirstName, user.LastName),
Selected = false,
Age = age.ToString(),
Sex = user.Sex
};
model.LGMembers.Add(editorViewModel);
}
model.Lifegroup.Add(lifegroup);
return View(model);
}
public ActionResult SubmitSelectedMembers(AddLifegroupMemberViewModel model)
{
var getSelected = (from members in model.LGMembers
where members.Selected
select members.Id);
List<string> _selected = new List<string>();
foreach (var selected in getSelected)
{
_selected.Add(selected);
}
//var selectedIds = model.getSelectedIds();
var lifegroupID = model.Lifegroup[0].LifegroupID;
//System.Diagnostics.Debug.WriteLine(string.Format("Lifegroup ID: {0}", model.Lifegroup.LifegroupID));
var selectedUsers = from u in db.Users
//where selectedIds.Contains(u.Id
where _selected.Contains(u.Id)
select u;
foreach (var users in selectedUsers)
{
var idManager = new IdentityManager();
idManager.AddUserToRole(users.Id, "LifegroupMember");
var lgmember = new LGMember()
{
LifegroupID = lifegroupID,
UserID = users.Id,
DateJoined = DateTime.Now
};
/*
* Insert Code to insert part of Lifegroup to GrowthMileStone
*/
db.LGMember.Add(lgmember);
}
db.SaveChanges();
return RedirectToAction("ViewLifegroups");
}
答案 0 :(得分:0)
在将对象插入LGMembers列表之前,需要实例化对象。这不会发生在上面的代码中。
答案 1 :(得分:0)
复选框输入null ViewModel的原因是,过滤或分页会将绑定视图切割为ViewModel。因为绑定无法看到发布时未显示的元素。虽然不是很好的解决方案,如果有人想以这种方式解决问题,解决这个问题的方法可能是在提交前删除过滤或分页。解决方案的示例放在表单提交
中table.search('').columns().search('').draw()
或
table.fnFilter("");
var oSettings = table.fnSettings();
table._iDisplayLength = -1;
table.fnDraw();