我有以下型号:
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; }
}
我想要每个学生和雇主各有多个经验。因此,一个stunt可以有任何数量的经验和甚至雇主也可以有任何号码。我面临的问题是,当它达到workexperience代码时,它说:
值不能为空。参数名称:source
但是,在传递IEnumerable模型Student
后,我在视图中获取所有值以下是视图代码
@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);
另外,我想告诉你雇主的观点是否合理。是具有相同代码的视图。这些关系是否未正确定义?或其他什么?
答案 0 :(得分:0)
查看您的代码,以下是您的流程的简化示例:
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
集合为空,那么您在最后.Count()
语句中调用if
而不进行空检查。
我认为最后一个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)
...
}