C# 使用EF连接MVC视图的表(模型)的正确方法,需要在数据列表和CRUD操作中使用

C# 使用EF连接MVC视图的表(模型)的正确方法,需要在数据列表和CRUD操作中使用,c#,entity-framework,asp.net-core-mvc,C#,Entity Framework,Asp.net Core Mvc,我的项目有一个简单的关系数据库。我正在尝试为列表视图集成表之间的连接,然后稍后填充表单数据和选择列表 我有一个团队类和一个地点类,都将LocationId作为FK共享。 我需要根据Teams表中LocationId的值显示团队信息和位置名称。这应该很容易,但我是EF的新手,不能让它工作 我为Teams类创建了一个ViewModel,并添加了一个位置列表,您将在下面的代码中看到 我遇到的两个问题是,视图中的位置没有结果,我不知道如何在LocationId上加入这个,所以如果它确实显示了一些内容,那

我的项目有一个简单的关系数据库。我正在尝试为列表视图集成表之间的连接,然后稍后填充表单数据和选择列表

我有一个团队类和一个地点类,都将LocationId作为FK共享。 我需要根据Teams表中LocationId的值显示团队信息和位置名称。这应该很容易,但我是EF的新手,不能让它工作

我为Teams类创建了一个ViewModel,并添加了一个位置列表,您将在下面的代码中看到

我遇到的两个问题是,视图中的位置没有结果,我不知道如何在LocationId上加入这个,所以如果它确实显示了一些内容,那么它将是错误的。 第二个问题是,即使我在视图中有可用的位置,我也无法深入到名称

我的位置课

public class Locations
{
    [Key]
    public int LocationId { get; set; }
    [Required(ErrorMessage = "Please enter a Location Name")]
    [MaxLength(50, ErrorMessage = "Name cannot exceed 50 characters")]
    public string Name { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    [Required]
    [Range(0, 99999, ErrorMessage = "Please enter a valid Zip Code")]
    public int ZipCode { get; set; }
    [Required(ErrorMessage = "Please enter a Phone Number")]
    [DataType(DataType.PhoneNumber)]
    [RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Please enter a valid Phone Number")]
    public string Telephone { get; set; }
    [Required(ErrorMessage = "Please enter an Email Address")]
    [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$",
        ErrorMessage = "The Email Address you have Entered is Invalid")]
    public string Email { get; set; }        
    public string Website { get; set; }
    [Required]
    [Display(Name = "Is Active?")]
    public bool IsActive { get; set; }
}
我的团队上课

public class TeamsViewModel
{
    [Key]
    public int TeamId { get; set; }
    [Required(ErrorMessage = "Please enter a Team Name")]
    [MaxLength(50, ErrorMessage = "The Team Name cannot exceed 50 characters")]
    public string Name { get; set; }
    [Required]
    [Range(1, 99999, ErrorMessage = "Please Select a Home Location")]
    [Display(Name = "Home Location")]
    public int LocationId { get; set; }
    [Required]
    [Display(Name = "Is Active?")]
    public bool IsActive { get; set; }        
    public List<Locations> Location { get; set; }
}
以及我的一部分观点

@foreach (var team in Model)
{
    <tr class="tblRow">
        <td>@team.Name</td>
        **<td>@team.Location</td>**
        <td>
            <a class="btn btn-outline-info m-1"
               asp-controller="admin" asp-action="editTeam" asp-route-id="@team.Location">
                Edit
            </a>
        </td>
        <td>
            @using (Html.BeginForm("DeleteTeam", "Admin", team))
            {
                <input type="submit" value="Delete"
                       class="btn btn-outline-danger m-1"
                       onclick="return confirm('Are you sure you wish to delete this Team?');" />
            }
        </td>
        <td class="activeStatus" style="display:none;">
            @team.IsActive
        </td>
    </tr>
}

应该简单到

var q = from t in db.Teams
        select new TeamsViewModel()
        {
          TeamId = t.TeamId,
          Name = t.Name,
          . . .
          Locations = t.Locations.ToList()
        };

var results = q.ToList();
您可以在linq中使用Include函数来获取与TeamsViewModel相关的位置列表

 public IActionResult Teams()
        {
            var model = _context.TeamsViewModel.Include(x => x.Location).ToList();
             return View("Teams", model);
        }
在视图中,需要向循环位置liststeam添加foreach以显示位置名称

  @foreach (var team in Model)
    {
        @foreach (var location in team.Location)
        {
            <tr class="tblRow">
                <td>@team.Name</td>
                <td>@location.Name</td>
                <td>
                    <a class="btn btn-outline-info m-1"
                       asp-controller="Team" asp-action="editTeam" asp-route-id="@location.LocationId">
                        Edit
                    </a>
                </td>
                <td>
                    @using (Html.BeginForm("DeleteTeam", "Team", team))
                    {
                        <input type="submit" value="Delete"
                               class="btn btn-outline-danger m-1"
                               onclick="return confirm('Are you sure you wish to delete this Team?');" />
                    }
                </td>

                <td class="activeStatus" style="display:none;">
                    @team.IsActive
                </td>
            </tr>
        }

    }

您能否共享您的团队模型而不是查看模型?
  @foreach (var team in Model)
    {
        @foreach (var location in team.Location)
        {
            <tr class="tblRow">
                <td>@team.Name</td>
                <td>@location.Name</td>
                <td>
                    <a class="btn btn-outline-info m-1"
                       asp-controller="Team" asp-action="editTeam" asp-route-id="@location.LocationId">
                        Edit
                    </a>
                </td>
                <td>
                    @using (Html.BeginForm("DeleteTeam", "Team", team))
                    {
                        <input type="submit" value="Delete"
                               class="btn btn-outline-danger m-1"
                               onclick="return confirm('Are you sure you wish to delete this Team?');" />
                    }
                </td>

                <td class="activeStatus" style="display:none;">
                    @team.IsActive
                </td>
            </tr>
        }

    }