Search code examples
c#asp.net-corerazor-pagesasp.net-core-2.2ef-core-2.2

Data of Multiple related tables not shown in Razor page


I am using .net core 2.2. When I run the following output is shown in the list of FacultyInterestItem with the Faculty or Keywords table data missing. Although the Faculties and Keywords classes are connected with the FacltyInterestItems Faculty Interest Items list with missing Faculty and Keyword Data

The following is the razor page

<tbody>
    @foreach (var item in Model.FacultyInterestItems)
    {
        <tr>
 <td>     @Html.DisplayFor(modelItem => item.Id)           
        </td>                
 <td>
                @Html.DisplayFor(modelItem => item.Faculty.FacultyId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Keyword.Name)
            </td>
            <td>
                <a asp-page="./Edit" asp-route-id="@item.Id">Edit</a> |
                <a asp-page="./Details" asp-route-id="@item.Id">Details</a> |
                <a asp-page="./Delete" asp-route-id="@item.Id">Delete</a>
            </td>
        </tr>
    }
</tbody>

The cs file of the Razor page having OnGetAsync() method where it connects and take data from WebAPI.

public async Task OnGetAsync()
{
    List<FacultyInterestItems> facultyInterestItem = new List<FacultyInterestItems>();
    HttpClient client = _api.Initial();
    HttpResponseMessage res = await client.GetAsync("api/FacultyInterestItems");
    if (res.IsSuccessStatusCode)
    {
        var result = res.Content.ReadAsStringAsync().Result;
        facultyInterestItem = JsonConvert.DeserializeObject<List<FacultyInterestItems>>(result);
    }
    List<Faculties> listOfFaculty = new List<Faculties>();
    res = await client.GetAsync("api/Faculties");
    if (res.IsSuccessStatusCode)
    {
        var result = res.Content.ReadAsStringAsync().Result;
        listOfFaculty = JsonConvert.DeserializeObject<List<Faculties>>(result);
    }
    List<Keywords> listOfKeywords = new List<Keywords>();
    res = await client.GetAsync("api/Keywords");
    if (res.IsSuccessStatusCode)
    {
        var result = res.Content.ReadAsStringAsync().Result;
        listOfKeywords = JsonConvert.DeserializeObject<List<Keywords>>(result);
    }
    FacultyInterestItems = facultyInterestItem;
    Keywords = listOfKeywords;
    Faculties = listOfFaculty;

}

The procedure OnGetAscyn() in razor page cs file, gets data from API. Here is the get method in the controller of API that connects to the DB and fetch data.

[HttpGet]
public async Task<ActionResult<IEnumerable<FacultyInterestItems>>> GetFacultyInterestItems()
{
    return await _context.FacultyInterestItems.ToListAsync();
}

Here is the model:

The Faculties Table

public partial class Faculties
{
    public Faculties()
    {
        FacultyInterestItems = new HashSet<FacultyInterestItems>();
        SuggestedKeywords = new HashSet<SuggestedKeywords>();
    }

    public long Id { get; set; }
    public string FacultyId { get; set; }
    public DateTime? UpdateDate { get; set; }
    public DateTime? InsertDate { get; set; }

    public virtual ICollection<FacultyInterestItems> FacultyInterestItems { get; set; }
    public virtual ICollection<SuggestedKeywords> SuggestedKeywords { get; set; }
}

The FacultyInterestItems tables:

public partial class FacultyInterestItems
{
    public long Id { get; set; }
    public long? FacultyId { get; set; }
    public int? KeywordId { get; set; }
    public DateTime InsertDate { get; set; }
    public DateTime UpdateDate { get; set; }

    public virtual Faculties Faculty { get; set; }
    public virtual Keywords Keyword { get; set; }
}

the Keywords table:

public partial class Keywords
{
    public Keywords()
    {
        FacultyInterestItems = new HashSet<FacultyInterestItems>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime InsertDate { get; set; }
    public DateTime UpdateDate { get; set; }
    public int? DepartmentId { get; set; }

    public virtual Departments Department { get; set; }
    public virtual ICollection<FacultyInterestItems> FacultyInterestItems { get; set; }
}

Data of Faculty and Keyword is not fetched from the DB. Please let me know the solution


Solution

  • Try to call Include method

     _context.FacultyInterestItems.Include(x => x.Faculty).Include(x => x.Keyword).ToListAsync()