简体   繁体   English

如何在此代码中传递“CourseTitle”和“FullName”?

[英]How to pass the "CourseTitle" and the "FullName" in this code?

I wanna to pass the name for the both of entity instead of the ID, The entity of "AppUser"我想传递两个实体的名称而不是 ID,“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"以及“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)索引页面的代码(这是SelectCourse的索引时代)

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

<div class="container-fluid py-4">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <p>Selected Courses</p>
                    <a asp-controller="SelectCourse" asp-action="Create" class="btn btn-success">Create</a>
                </div>
                <div class="card-body">
                    <table class="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" class="btn btn-primary">Edit</a> |
                                        <a asp-controller="SelectCourse" asp-action="Details" asp-route-id="@item.Id" class="btn btn-warning">Details</a> |
                                        <a asp-controller="SelectCourse" asp-action="Delete" asp-route-id="@item.Id" class="btn btn-danger">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. ..................................... 基于上述index[dot]cshtml age的代码,返回Cours的Id和AppUser,我想显示两个实体的名称。

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.正如 Metro Smurf 所说,您可以添加两个属性来存储两个实体的名称,然后从 controller 获取它们并返回以查看页面。 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 .此外,从您的代码来看,AppUser 和课程 class 配置为多对多关系 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.所以,在controller中,可以使用Include方法加载相关实体,参见加载相关数据,然后可以得到SelectCourse的列表,其中包含相关的AppUser和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" class="btn btn-primary">Edit</a> |
            <a asp-controller="SelectCourse" asp-action="Details" asp-route-id="@item.Id" class="btn btn-warning">Details</a> |
            <a asp-controller="SelectCourse" asp-action="Delete" asp-route-id="@item.Id" class="btn btn-danger">Delete</a>
        </td>
    </tr>
}

Note: In this scenario, the return model is SelectCourse, instead of SelectCourseDto.注意:在这个场景中,返回的model是SelectCourse,而不是SelectCourseDto。

More detail information, see Tutorial: Read related data - ASP.NET MVC with EF Core .更多详细信息,请参见教程:阅读相关资料 - ASP.NET MVC with EF Core

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM