Home > Mobile >  How to pass the "CourseTitle" and the "FullName" in this code?
How to pass the "CourseTitle" and the "FullName" in this code?

Time:04-18

I wanna to pass the name for the both of entity instead of the ID, The entity of "AppUser"

public class AppUser
{
    public int Id { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string Password { get; set; }

    public string UserName { get; set; }


    [NotMapped]
    public string FullName => $"{FirstName} {LastName}";

    public virtual ICollection<SelectCourse> SelectCourses { get; set; }
}

The entity of "Course"

public class Course
    {
        public int Id { get; set; }

        public string CourseName { get; set; }

        public string CourseSubject { get; set; }

        public string Content { get; set; }
        


        public int TeacherId { get; set; }

        public virtual Teacher Teacher { get; set; }



        public int CategoryCourseId { get; set; }

        public CategoryCourse CategoryCourse { get; set; }



        public ICollection<SelectCourse> SelectCourses { get; set; }

    }

And the entity of "SelectCourse"

public class SelectCourse
{
    public int Id { get; set; }



    public AppUser AppUser { get; set; }
   
    public int AppUserId { get; set; }




    public Course Course { get; set; }

    public int CourseId { get; set; }
}

The Code of the Index page (This is the index age of SelectCourse)

 @model IEnumerable<SelectCourseDto>
    @{
        ViewData["Title"] = "Selected Courses List";
    }

<div >
    <div >
        <div >
            <div >
                <div >
                    <p>Selected Courses</p>
                    <a asp-controller="SelectCourse" asp-action="Create" >Create</a>
                </div>
                <div >
                    <table >
                        <thead>
                            <tr>
                                <th><a asp-controller="SelectCourse" asp-action="OrderByFirstName">Course Title</a></th>
                                <th><a asp-controller="SelectCourse" asp-action="OrderByFirstName">User</a></th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var item in Model)
                            {
                                <tr>
                                    
                                    <td>@item.CourseId</td>
                                    <td>@item.AppUserId</td>
                                    <td>
                                        <a asp-controller="SelectCourse" asp-action="Edit" asp-route-id="@item.Id" >Edit</a> |
                                        <a asp-controller="SelectCourse" asp-action="Details" asp-route-id="@item.Id" >Details</a> |
                                        <a asp-controller="SelectCourse" asp-action="Delete" asp-route-id="@item.Id" >Delete</a>
                                    </td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

And this is the Controller 

        [HttpGet]
        public IActionResult Index()
        {
            var results = _selectCourseService.GetAll();

            var resultDtos = results.Select(pr => new SelectCourseDto()
            {
                Id = pr.Id,
                AppUserId = pr.AppUserId,
                CourseId = pr.CourseId
            });


            return View(resultDtos);
        }

............................ Based on the above code of index[dot]cshtml age, it returns the Id of the Cours and the AppUser, I wanna to show the name of the both entities.

CodePudding user response:

As Metro Smurf said, you could add two properties to store the name of both entities, then, get them from the controller and return to view the page. This might be the easiest way to do that.

Besides, from your code, the AppUser and the Course class was configured with many to many relationship. So, in the controller, you could load the related entities using the Include method, see Loading Related Data, then you can get a list of SelectCourse, which contains the related AppUser and Course.

Then, in the view page, to display the related entity's property, you could use the navigation property, so, try to modify the code as below:

@model IEnumerable<SelectCourse>
...

@foreach (var item in Model)
{
    <tr>
        
        <td>@item.Course.CourseName</td>
        <td>@item.AppUser.UserName</td>
        <td>
            <a asp-controller="SelectCourse" asp-action="Edit" asp-route-id="@item.Id" >Edit</a> |
            <a asp-controller="SelectCourse" asp-action="Details" asp-route-id="@item.Id" >Details</a> |
            <a asp-controller="SelectCourse" asp-action="Delete" asp-route-id="@item.Id" >Delete</a>
        </td>
    </tr>
}

Note: In this scenario, the return model is SelectCourse, instead of SelectCourseDto.

More detail information, see Tutorial: Read related data - ASP.NET MVC with EF Core.

  • Related