【问题标题】:ASP.NET MVC ( Object reference not set to an instance of an object )ASP.NET MVC(对象引用未设置为对象的实例)
【发布时间】:2020-04-13 06:41:20
【问题描述】:

显示个人文件列表时出现以下问题, 但我在查询中使用了(使用 System.Data.Entity)和(包括)它不起作用

类模型

public class PersonalFile
{
    public int Id { get; set; }

    [StringLength(50)]
    public string FileName { get; set; }

    [StringLength(500)]
    public string FilePath { get; set; }

    public DateTime UploadDateTime { get; set; }

    public int EmployeeId { get; set; }

    [ForeignKey("EmployeeId")]
    public Employee Employee { get; set; }

    public int? FileCategoryId { get; set; }
    [ForeignKey("FileCategoryId")]
    public virtual FileCategory FileCategory { get; set; }

}

类视图模型

public class PersonalFileViewModel
{
    public int Id { get; set; }

    [StringLength(50)]
    public string FileName { get; set; }

    [StringLength(500)]
    public string FilePath { get; set; }
    public int EmployeeId { get; set; }

    [Required]
    public int FileCategoryId { get; set; }

    public IEnumerable<PersonalFile> PersonalFiles { get; set; }
}

类模型文件类别

public class FileCategory
{
    public int FileCategoryId { get; set; }

    public string categoryName { get; set; }
    public string optionGroup { get; set; }
    public virtual ICollection<PersonalFile> PersonalFile { get; set; }
}

动作方法 公共 ActionResult 索引(int?id) { 如果(id == null) 返回 HttpNotFound();

        var empl = _dbContext.Employees.SingleOrDefault(c => c.EmployeeId == id);
        if (empl == null)
            return HttpNotFound();

        ViewBag.FileCategoryId = new SelectList(_dbContext.FileCategory, "FileCategoryId", "categoryName" , "optionGroup", 0);

        var vwModel = new PersonalFileViewModel
        {
            EmployeeId = empl.EmployeeId,
            PersonalFiles = _dbContext.PersonalFiles.Include(p => p.Employee).Include(p => p.FileCategory).Where(x => x.EmployeeId == id)
        };


        NameAndImage(empl.EmployeeId);
        return View(vwModel);
    }

查看

foreach (var j in Model.PersonalFiles)
                            {
                                <tr style="border-top: 1px solid black;">
                                    <td style="vertical-align: middle;">@j.FileName</td>
                                    <td style="vertical-align: middle;"> @j.FileCategory.categoryName </td>
                                    <td style="vertical-align: middle;">@j.UploadDateTime</td>
                                    <td>
                                        <a href=@Url.Content("~/Content/Images/PersonalFiles/"+j.FilePath) class="btn btn-success btn-sm">Download</a>
                                        @Html.ActionLink("Delete", "Delete", new { j.Id }, new { @class = "btn btn-danger btn-sm", onclick = "return confirm('Do you really want to delete this file?');" })
                                    </td>
                                </tr>

                            }

enter image description here

【问题讨论】:

  • 检查@j.FileCategory 是否为空?也许这就是为什么它没有检索 categoryName 的值
  • 在您的控制器中添加一个临时行:_dbContext.PersonalFiles.Include(p =&gt; p.Employee).Include(p =&gt; p.FileCategory).Where(x =&gt; x.EmployeeId == id).ToList(); - 这里是预期的数据吗?它在数据库中吗?

标签: asp.net-mvc join include


【解决方案1】:

我通过将这一行代码放在视图上解决了这个问题

@(j.FileCategory == null ? "" : j.FileCategory.categoryName)

【讨论】:

    【解决方案2】:

    首先,确保每个PersonalFile都相应地属于FileCategory

    其次,您应该.ToList() 将我们的查询强制改为execute immediately

    PersonalFiles = _dbContext.PersonalFiles.Include(p => p.Employee).Include(p => p.FileCategory).Where(x => x.EmployeeId == id).ToList()
    

    【讨论】:

      猜你喜欢
      • 2014-10-15
      • 1970-01-01
      • 1970-01-01
      • 2013-07-26
      • 2013-10-28
      • 1970-01-01
      • 2018-09-07
      • 1970-01-01
      • 2021-09-14
      相关资源
      最近更新 更多