【发布时间】: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>
}
【问题讨论】:
-
检查@j.FileCategory 是否为空?也许这就是为什么它没有检索 categoryName 的值
-
在您的控制器中添加一个临时行:
_dbContext.PersonalFiles.Include(p => p.Employee).Include(p => p.FileCategory).Where(x => x.EmployeeId == id).ToList();- 这里是预期的数据吗?它在数据库中吗?
标签: asp.net-mvc join include