I have the following problem when displaying list of personal file, but I used the (using System.Data.Entity) and (Include) in the query it's not working
Class Model
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; }
}
Class View Model
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; }
}
Class Model File Category
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; }
}
Action Method public ActionResult Index(int? id) { if (id == null) return 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);
}
View
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 [email protected]("~/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>
}
_dbContext.PersonalFiles.Include(p => p.Employee).Include(p => p.FileCategory).Where(x => x.EmployeeId == id).ToList();- is the expected data in here? Is it in the database? - Steve Greene