【发布时间】:2017-01-21 20:34:14
【问题描述】:
我有以下型号:
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; }
}
我希望每个学生和雇主都有多种工作经验。因此,学生可以拥有任意数量的经验,甚至雇主也可以拥有任意数量的经验。我面临的问题是,当它到达工作体验代码时,它会说:
值不能为空。参数名称:来源
但是,在传递模型 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);
另外我想告诉你,雇主视图工作正常,并且是具有相同代码的视图。关系没有正确定义吗?还是别的什么?
【问题讨论】:
-
哪一行代码会抛出异常?
-
您的代码在您的视图中有语法错误。 if 语句中的
@符号必须是这样的:@if (...)而不是if(@...) -
@CodingYoshi,它不是语法错误,它必须是
if(...)-@if(...)会抛出异常(student前面的@毫无意义) -
@Stephen 最后一个 if 循环的右括号 } 抛出错误。
-
不,它没有。但是您甚至没有显示相关代码。你有一个
count++;和if (@student.WorkExperiences.Count() == count),但你甚至没有在哪里为count初始化变量并且代码无法编译。它不可能重现您的问题。
标签: c# asp.net-mvc entity-framework ef-code-first code-first