我像这样将对象集合发送到我的视图模型
控制器
SoundsViewModel model = new SoundsViewModel(sound, AllGroups());
视图模型
public SoundsViewModel(sound, List<LocationGroup> allGroups)
{
....
Groups = new List<SoundLocationGroupViewModel>();
if (allGroups !=null)
{
foreach (var item in allGroups)
{
var i = (from r in sound.LocationGroups where r.Id == item.Id select r).FirstOrDefault();
Groups.Add(new SoundLocationGroupViewModel() { LocationGroupId = item.Id, LocationGroupName = item.Name, IsInLocationGroup = (i != null) });
}
}
}
错误信息是
Value cannot be null.
Parameter name: source
var i = (from r in sound.LocationGroups where r.Id == item.Id select r).FirstOrDefault();
显然,此消息在描述情况下似乎是完美且清晰的,但在调试时,我正在填充 allGroups 集合并在视图模型上正确接收。
任何想法,已经很晚了,如果有人建议我提示如何在明天早上的咖啡中解决这个问题,我将不胜感激。
谢谢
更新:现在它通过了。
foreach (var item in allGroups)
{
if(sound.LocationGroups !=null)
{
var i = (from r in sound.LocationGroups where r.Id == item.Id select r).FirstOrDefault();
Groups.Add(new SoundLocationGroupViewModel() { LocationGroupId = item.Id, LocationGroupName = item.Name, IsInLocationGroup = (i != null) });
}
}