vinicius01cs
vinicius01cs

Reputation: 27

How to show contents from other Models in my view using .NET Core 6?

I have an Index view that should display the name of each person's course, however, in the model used there is only the Course Id, the course name is in another model - CursosModel - and in another table in the database. How can I display the course name in my view?

Index view:

@model IEnumerable<FeedbackUnivicosa.Models.Professor>

@{
    ViewData["Title"] = "Professores";
}

<h4>@ViewData["Title"]</h4>

<form asp-action="Index" method="get">
    <div class="form-actions no-color">
        <p>
            <input type="text" name="SearchString" class="inputSearch" value="@ViewData["CurrentFilter"]" />
            <button type="submit" class="botaoSearch fa fa-solid fa-magnifying-glass"></button>
            <a asp-action="Index"><i class="espacoFiltro fa fa-solid fa-filter-circle-xmark"></i></a>
            <a asp-action="Create" class="botaoCadastro btn btn-primary btn-sm">Cadastrar</a>
        </p>
    </div>
</form>
<table class="table">
    <thead>
        <tr>
            <th></th>
            <th>
                <a asp-action="Index" asp-route-sortOrder="@ViewData["NameSortParm"]">Professor</a> 
            </th>
            <th>
                <a asp-action="Index" asp-route-sortOrder="@ViewData["EmailSortParm"]">Email</a>
            </th>
            <th>
                <a asp-action="Index" asp-route-sortOrder="@ViewData["CursoSortParm"]">Curso</a>
            </th>
           
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model) {
        <tr>
            <td class="fa-lg">
                    <a asp-action="Edit" asp-route-id="@item.Id"><i class="espacoIcone fa-solid fa-user-pen"></i></a>
                    
                    <a asp-action="Delete" asp-route-id="@item.Id"><i class="espacoIcone fa-solid fa-trash-can"></i></a>       
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Nome)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.EmailProfessor)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CursoId)
            </td>
           
        </tr>
}
    </tbody>
</table>

I am trying to show the course name on the Index view.

Upvotes: 1

Views: 63

Answers (2)

Xinran Shen
Xinran Shen

Reputation: 9993

From your description of this question, I think you can inject your DBContext into your view and get the value of course name from CursosModel table, Please refer to this simple demo:

Model:

public class CursosModel
    {
        public int id { get; set; }
        public string CursoName { get; set; }
    }

public class Professor
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int CourseId { get; set; }
    }

Then in the view, You can inject dbcontext into your view then get the course Name by Professor's CourseId.

@using xxxx;
@model IEnumerable<Professor>
@inject ApplicationDbContext dbcontext

@foreach (var item in Model)
{
    <tr>
        <td class="fa-lg">
            <a asp-action="Edit" asp-route-id="@item.Id"><i class="espacoIcone fa-solid fa-user-pen"></i></a>

            <a asp-action="Delete" asp-route-id="@item.Id"><i class="espacoIcone fa-solid fa-trash-can"></i></a>
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CourseId)
        </td>
        <td>
           @dbcontext.cursosModels.Where(x => x.id == item.CourseId).Select(y => y.CursoName).FirstOrDefault();       
        </td>
        <br>
    </tr>
}

Demo:

enter image description here

Upvotes: 2

Paul
Paul

Reputation: 218

because the Microsoft website teaches Add the model so after you can reference the Microsoft site.
reference Microsoft website

public class CursosModel
    {
        public List<CursosModel> CursosModel { get; set; }
        public List<Course> Course{ get; set; }
    }

Upvotes: -1

Related Questions