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



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

我有一个团队类和一个地点类,两者都以FK共享LocationId。我需要根据Teams表中LocationId的值显示团队信息和位置的NAME。这应该很容易,但我是EF的新手,无法让它发挥作用。

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

我遇到的两个问题是,视图中没有Location的结果(我不知道如何在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; }
}

我的控制器对此页面的操作

public IActionResult Teams()
{
var model = _teamRepository.GetAllTeams();
return View("Teams", model);
}

我的一部分观点

@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>
}

您可以使用linq中的Include函数来获取与TeamsViewModel相关的位置列表。

public IActionResult Teams()
{
var model = _context.TeamsViewModel.Include(x => x.Location).ToList();
return View("Teams", model);
}

在视图中,您需要向循环位置列表(团队(添加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>
}
}

应该像一样简单

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

最新更新