如何在这段代码中传递 "CourseTitle" 和 "FullName"?
How to pass the "CourseTitle" and the "FullName" in this code?
我想传递两个实体的名称而不是 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; }
}
“课程”实体
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; }
}
以及“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; }
}
索引页面的代码(这是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);
}
.......................
基于上面index[dot]cshtml age的代码,它returns课程的Id和AppUser,我想显示两个实体的名称。
正如 Metro Smurf 所说,您可以添加两个属性来存储两个实体的名称,然后从控制器中获取它们并 return 以查看页面。这可能是最简单的方法。
此外,从你的代码中,AppUser和Course class配置了many to many relationship. So, in the controller, you could load the related entities using the Include method, see Loading Related Data,那么你可以得到一个SelectCourse的列表,其中包含相关的AppUser和Course。
然后,在视图页面中,要显示相关实体的属性,您可以使用导航属性,所以,尝试修改代码如下:
@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>
}
注意:在这种情况下,return模型是SelectCourse,而不是SelectCourseDto。
更多详细信息,请参阅Tutorial: Read related data - ASP.NET MVC with EF Core。
我想传递两个实体的名称而不是 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; }
}
“课程”实体
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; }
}
以及“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; }
}
索引页面的代码(这是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);
}
....................... 基于上面index[dot]cshtml age的代码,它returns课程的Id和AppUser,我想显示两个实体的名称。
正如 Metro Smurf 所说,您可以添加两个属性来存储两个实体的名称,然后从控制器中获取它们并 return 以查看页面。这可能是最简单的方法。
此外,从你的代码中,AppUser和Course class配置了many to many relationship. So, in the controller, you could load the related entities using the Include method, see Loading Related Data,那么你可以得到一个SelectCourse的列表,其中包含相关的AppUser和Course。
然后,在视图页面中,要显示相关实体的属性,您可以使用导航属性,所以,尝试修改代码如下:
@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>
}
注意:在这种情况下,return模型是SelectCourse,而不是SelectCourseDto。
更多详细信息,请参阅Tutorial: Read related data - ASP.NET MVC with EF Core。