【问题标题】:Value cannot be null parameter name: source even when IEnumerable in not null值不能为 null 参数名称:即使 IEnumerable 不为 null 时也是如此
【发布时间】: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


【解决方案1】:

查看您的代码,这是您的流程的简化示例:

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 集合为空,那么您将在最后一个if 语句中对其调用.Count(),而之前没有进行空检查。

我认为最后一个 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)
    ...
}

【讨论】:

  • 谢谢,但现在代码正在运行我仍然不明白是什么使它工作,因为它与以前的代码相同。我注意到的唯一一件事是在调试视图时,如果您在集合中获得的第一个键值是 null,那么即使您在循环中没有访问该值,它也会抛出。但现在我将简化我的循环,让它看起来更整洁。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多