2

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>

                            }

enter image description here

2
  • check if @j.FileCategory is coming null? perhaps thats why it is not retrieving the value of categoryName Commented Dec 20, 2019 at 20:17
  • Add a temporary line in your controller: _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? Commented Dec 20, 2019 at 20:40

2 Answers 2

0

Firstly, make sure that with each PersonalFile belongs to FileCategory accordingly.

Secondly, You should .ToList() to force our query to execute immediately instead.

PersonalFiles = _dbContext.PersonalFiles.Include(p => p.Employee).Include(p => p.FileCategory).Where(x => x.EmployeeId == id).ToList()
Sign up to request clarification or add additional context in comments.

Comments

0

I solved the problem by putting this line Code on the View

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.