值不能为空参数名称:源即使 IEnumerable 不为空
Value cannot be null parameter name: source even when IEnumerable in not null
我有以下型号:
public class Student
{
public int Id { get; set; }
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
public string LastName { get; set; }
public ICollection<WorkExperience> WorkExperiences { get; set; }
public ICollection<Education> Educations { get; set; }
public ICollection<Skill> Skills { get; set; }
}
public class Employer
{
public int Id { get; set; }
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
public string LastName { get; set; }
public ICollection<WorkExperience> WorkExperiences { get; set; }
public ICollection<Education> Educations { get; set; }
public ICollection<Skill> Skills { get; set; }
}
public class WorkExperience
{
public int Id { get; set; }
[Display(Name = "Company Name")]
public string CompanyName { get; set; }
[Display(Name = "Job Title")]
public string JobTitle { get; set; }
[Display(Name = "Experience (in years)")]
public string Experience { get; set; }
[Display(Description = "Enter City, Country for e.g. San Francisco, California")]
public string Location { get; set; }
public virtual ICollection<Employer> Employers { get; set; }
[Display(Name = "Set as Current Company")]
public virtual ICollection<Student> Students { get; set; }
}
我希望每个学生和雇主都拥有多种工作经验。因此,学生可以拥有任意数量的经验,甚至雇主也可以拥有任意数量的经验。我面临的问题是,当它到达工作经验代码时,它说:
Value cannot be null. Parameter name: source
但是,在传递模型 Student
的 IEnumerable 后,我得到了视图中的所有值
以下是查看代码
@model IEnumerable<OpenOpportunity.Models.Student>
@foreach (var student in Model)
{
<div class="row clearfix">
<div class="col-xs-12">
<h1>
@student.FirstName @student.LastName
</h1>
</div>
</div>
var count = 0;
if (@student.WorkExperiences == null)
{
<small>No current position & company set</small>
<a id="currentDetails" href="@Url.Action("EditCurrentDetails", "Main", new { area = "Students", id = student.Id })">
<i class="fa fa-edit"></i>
</a>
}
else if (@student.WorkExperiences.Count() > 0)
{
foreach (var work in student.WorkExperiences)
{
if (work.IsCurrentCompany == true)
{
<small>@work.JobTitle,@work.CompanyName</small>
<a id="currentDetails" href="@Url.Action("EditCurrentDetails", "Main", new { area = "Students", id = student.Id })">
<i class="fa fa-edit"></i>
</a>
}
else
{
count++;
}
}
}
if (@student.WorkExperiences.Count() == count)
{
<small>No current position & company set</small>
<a id="currentDetails" href="@Url.Action("EditCurrentDetails", "Main", new { area = "Students", id = student.Id })">
<i class="fa fa-edit"></i>
</a>
}
}
控制器代码如下:
var userID = db.Users.Find(User.Identity.GetUserId());
var getStudentDetails = (from x in db.Students.Include("WorkExperiences")
where x.Id == userID.Student.Id
select x).ToList();
return View(getStudentDetails);
此外,我想告诉您,雇主视图工作正常并且是具有相同代码的视图。这些关系是否没有正确定义?还是别的?
查看您的代码,这里有一个简化的流程示例:
var count = 0;
if (@student.WorkExperiences == null)
{
<small>No current position & company set</small>
...
}
else if (@student.WorkExperiences.Count() > 0)
{
foreach (var work in student.WorkExperiences)
...
}
if (@student.WorkExperiences.Count() == count)
{
<small>No current position & company set</small>
...
}
看到问题了吗?如果 WorkExperiences
集合为 null,那么您在最后一个 if
语句中调用 .Count()
之前没有进行 null 检查。
我想这最后一个 if
语句是一些糟糕的遗留问题,您应该简单地删除它,以便您的流程变成这样:
var count = 0;
if (student.WorkExperiences == null || !student.WorkExperiences.Any())
{
<small>No current position & company set</small>
...
}
else
{
foreach (var work in student.WorkExperiences)
...
}
我有以下型号:
public class Student
{
public int Id { get; set; }
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
public string LastName { get; set; }
public ICollection<WorkExperience> WorkExperiences { get; set; }
public ICollection<Education> Educations { get; set; }
public ICollection<Skill> Skills { get; set; }
}
public class Employer
{
public int Id { get; set; }
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
public string LastName { get; set; }
public ICollection<WorkExperience> WorkExperiences { get; set; }
public ICollection<Education> Educations { get; set; }
public ICollection<Skill> Skills { get; set; }
}
public class WorkExperience
{
public int Id { get; set; }
[Display(Name = "Company Name")]
public string CompanyName { get; set; }
[Display(Name = "Job Title")]
public string JobTitle { get; set; }
[Display(Name = "Experience (in years)")]
public string Experience { get; set; }
[Display(Description = "Enter City, Country for e.g. San Francisco, California")]
public string Location { get; set; }
public virtual ICollection<Employer> Employers { get; set; }
[Display(Name = "Set as Current Company")]
public virtual ICollection<Student> Students { get; set; }
}
我希望每个学生和雇主都拥有多种工作经验。因此,学生可以拥有任意数量的经验,甚至雇主也可以拥有任意数量的经验。我面临的问题是,当它到达工作经验代码时,它说:
Value cannot be null. Parameter name: source
但是,在传递模型 Student
的 IEnumerable 后,我得到了视图中的所有值以下是查看代码
@model IEnumerable<OpenOpportunity.Models.Student>
@foreach (var student in Model)
{
<div class="row clearfix">
<div class="col-xs-12">
<h1>
@student.FirstName @student.LastName
</h1>
</div>
</div>
var count = 0;
if (@student.WorkExperiences == null)
{
<small>No current position & company set</small>
<a id="currentDetails" href="@Url.Action("EditCurrentDetails", "Main", new { area = "Students", id = student.Id })">
<i class="fa fa-edit"></i>
</a>
}
else if (@student.WorkExperiences.Count() > 0)
{
foreach (var work in student.WorkExperiences)
{
if (work.IsCurrentCompany == true)
{
<small>@work.JobTitle,@work.CompanyName</small>
<a id="currentDetails" href="@Url.Action("EditCurrentDetails", "Main", new { area = "Students", id = student.Id })">
<i class="fa fa-edit"></i>
</a>
}
else
{
count++;
}
}
}
if (@student.WorkExperiences.Count() == count)
{
<small>No current position & company set</small>
<a id="currentDetails" href="@Url.Action("EditCurrentDetails", "Main", new { area = "Students", id = student.Id })">
<i class="fa fa-edit"></i>
</a>
}
}
控制器代码如下:
var userID = db.Users.Find(User.Identity.GetUserId());
var getStudentDetails = (from x in db.Students.Include("WorkExperiences")
where x.Id == userID.Student.Id
select x).ToList();
return View(getStudentDetails);
此外,我想告诉您,雇主视图工作正常并且是具有相同代码的视图。这些关系是否没有正确定义?还是别的?
查看您的代码,这里有一个简化的流程示例:
var count = 0;
if (@student.WorkExperiences == null)
{
<small>No current position & company set</small>
...
}
else if (@student.WorkExperiences.Count() > 0)
{
foreach (var work in student.WorkExperiences)
...
}
if (@student.WorkExperiences.Count() == count)
{
<small>No current position & company set</small>
...
}
看到问题了吗?如果 WorkExperiences
集合为 null,那么您在最后一个 if
语句中调用 .Count()
之前没有进行 null 检查。
我想这最后一个 if
语句是一些糟糕的遗留问题,您应该简单地删除它,以便您的流程变成这样:
var count = 0;
if (student.WorkExperiences == null || !student.WorkExperiences.Any())
{
<small>No current position & company set</small>
...
}
else
{
foreach (var work in student.WorkExperiences)
...
}