我有一个Musico的模型和一个公社的模型,我试着把它们都带到这里来。我试着使用** Tuple **和其他东西,但是我回到了基础,这就是
public class ComunaViewModel
{
public Comuna ComunaDTO { get; set; }
public Musico MusicoDTO { get; set; }
}musico
public class Musico
{
[Key]
public int Id { get; set; }
public string Nombre { get; set; }
public string Estilos { get; set; }
public int IdComuna { get; set; }
public string Email { get; set; } 科莫纳
public class Comuna
{
[Key]
public int Id { get; set; }
public string Nombre { get; set; }--
public async Task<IActionResult> Index()
{
return View(await _context.Musico.ToListAsync());
}视图
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.MusicoDTO.Nombre)
</th>
<th>
@Html.DisplayNameFor(model => model.MusicoDTO.Estilos)
</th>
<th>
@Html.DisplayNameFor(model => model.ComunaDTO.Nombre)
----
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.MusicoDTO.Nombre)
</td>
<td>
@Html.DisplayFor(modelItem => item.MusicoDTO.Estilos)
</td>
<td>
@Html.DisplayFor(modelItem => item.ComunaDTO.Nombre)发布于 2021-10-11 07:49:49
您需要将List<ComunaViewModel>而不是List<Musico>传递给视图。您可以尝试将List<Musico>转换为List<ComunaViewModel>。
public async Task<IActionResult> Index()
{
List<Musico> l = await _context.Musico.ToListAsync();
List<ComunaViewModel> l1 = new List<ComunaViewModel>();
foreach (var item in l)
{
l1.Add(new ComunaViewModel { MusicoDTO = item });
}
return View(l1);
}https://stackoverflow.com/questions/69511239
复制相似问题