Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 值不能为null参数名称:source,即使IEnumerable处于非null状态_C#_Asp.net Mvc_Entity Framework_Ef Code First_Code First - Fatal编程技术网

C# 值不能为null参数名称:source,即使IEnumerable处于非null状态

C# 值不能为null参数名称:source,即使IEnumerable处于非null状态,c#,asp.net-mvc,entity-framework,ef-code-first,code-first,C#,Asp.net Mvc,Entity Framework,Ef Code First,Code First,我有以下型号: 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<WorkExper

我有以下型号:

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; }
}

另外,我想告诉你,雇主视图工作正常&是具有相同代码的视图。这些关系是否没有正确定义?还是别的什么?

看看你的代码,这里有一个简化的流程示例:

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>
    ...
}

哪行代码引发该异常?您的代码在视图中有语法错误。if语句中的
@
符号需要如下:
@if(…)
不是
如果(@…
@CodingYoshi,它不是语法错误,它需要是
如果(…)
-
@if(…)
会抛出异常(学生面前的
@
只是毫无意义)@Stephen最后一个if循环的闭括号}抛出错误。不,它没有。但您甚至没有显示相关代码。你有一个
count++
if(@student.WorkExperiences.Count()==Count)
但是你甚至没有在哪里为
Count
初始化一个变量,并且代码没有编译。这是不可能重现你的问题。谢谢,但现在的代码是工作的,我仍然不明白是什么使它工作,因为它是相同的代码,因为以前。我注意到的唯一一件事是,在视图中调试时,如果您在集合中获得的第一个键值为null,那么它将抛出,即使您没有在循环中的该点访问该值。但现在我将简化我的循环,使其看起来整洁。
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>
    ...
}
var count = 0;
if (student.WorkExperiences == null || !student.WorkExperiences.Any())
{
    <small>No current position & company set</small>
    ...
}
else
{
    foreach (var work in student.WorkExperiences)
    ...
}