我想显示一个带有表的视图,该表显示基于另一个表中id的元素。下面是我要关联的两个表的示例。
Name_tbl
-id
-Name
Measurements_tbl
id
idName (i already have foreign key setup in SSMS)
所以,我想要得到的结果类似于以下查询:
Select * from Measurements_tbl
where idName=‘1'
result:
id | MeasureDate | idName | Measure1 | Measure2
1 12/15/2018 1 10 15
2 12/20/2018 1 25 30
3 12/25/2018 1 20 18
这是我现在拥有的设置:
ViewModel
public class DetalleNombreMedidasViewModel
{
public Name_tbl Names { get; set; }
public IEnumerable<Measurements_tbl> Measures { get; set; }
}
控制器
public ActionResult Details(int id)
{
var name = _context.Name_tbl.Include(p=> p.Measures_tbl).FirstOrDefault(p=>p.id==id);
var measures = patient.Measures_tbl;
var viewModel = new DetalleNombreMedidasViewModel
{
Names = name,
Measures = measures
};
return View(viewModel);
}
查看
@model FahWebApp.ViewModels.DetalleNombreMedidasViewModel
@{
ViewBag.Title = "Detalles";
Layout = "~/Views/Shared/_Layout.cshtml";
}
这是我想要获得的视图:
@foreach(var name in Model)
{
<tr>
<td> @name.Measure1 </td>
<td> @name.Measure2 </td>
</tr>
}
DbModel
namespace FahWebApp.Models
{
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
public partial class DbModel : DbContext
{
public DbModel()
: base("name=DbModel")
{
}
public virtual DbSet<Direccion_tbl> Direccion_tbl { get; set; }
public virtual DbSet<Estado_cat> Estado_cat { get; set; }
public virtual DbSet<Medidas_tbl> Medidas_tbl { get; set; }
public virtual DbSet<Nombre_tbl> Nombre_tbl { get; set; }
public virtual DbSet<Pais_cat> Pais_cat { get; set; }
public virtual DbSet<database_firewall_rules> database_firewall_rules { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Direccion_tbl>()
.Property(e => e.Email)
.IsUnicode(false);
modelBuilder.Entity<Direccion_tbl>()
.Property(e => e.Calle)
.IsUnicode(false);
modelBuilder.Entity<Direccion_tbl>()
.Property(e => e.Colonia)
.IsUnicode(false);
modelBuilder.Entity<Direccion_tbl>()
.Property(e => e.Ciudad)
.IsUnicode(false);
modelBuilder.Entity<Direccion_tbl>()
.Property(e => e.CodigoPostal)
.IsUnicode(false);
modelBuilder.Entity<Direccion_tbl>()
.Property(e => e.TelefonoCasa)
.IsUnicode(false);
modelBuilder.Entity<Direccion_tbl>()
.Property(e => e.TelefonoCelular)
.IsUnicode(false);
modelBuilder.Entity<Estado_cat>()
.Property(e => e.Estado)
.IsUnicode(false);
modelBuilder.Entity<Estado_cat>()
.HasMany(e => e.Direccion_tbl)
.WithRequired(e => e.Estado_cat)
.HasForeignKey(e => e.idEstado)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Medidas_tbl>()
.Property(e => e.Pecho)
.HasPrecision(10, 2);
modelBuilder.Entity<Medidas_tbl>()
.Property(e => e.Bicep)
.HasPrecision(10, 2);
modelBuilder.Entity<Medidas_tbl>()
.Property(e => e.Antebrazo)
.HasPrecision(10, 2);
modelBuilder.Entity<Medidas_tbl>()
.Property(e => e.Muñeca)
.HasPrecision(10, 2);
modelBuilder.Entity<Medidas_tbl>()
.Property(e => e.Cintura)
.HasPrecision(10, 2);
modelBuilder.Entity<Medidas_tbl>()
.Property(e => e.Cadera)
.HasPrecision(10, 2);
modelBuilder.Entity<Medidas_tbl>()
.Property(e => e.Muslo)
.HasPrecision(10, 2);
modelBuilder.Entity<Medidas_tbl>()
.Property(e => e.Pantorrilla)
.HasPrecision(10, 2);
modelBuilder.Entity<Nombre_tbl>()
.Property(e => e.Nombre)
.IsUnicode(false);
modelBuilder.Entity<Nombre_tbl>()
.Property(e => e.Apellido)
.IsUnicode(false);
modelBuilder.Entity<Nombre_tbl>()
.Property(e => e.Sexo)
.IsUnicode(false);
modelBuilder.Entity<Nombre_tbl>()
.Property(e => e.Altura)
.HasPrecision(3, 2);
modelBuilder.Entity<Nombre_tbl>()
.HasMany(e => e.Direccion_tbl)
.WithRequired(e => e.Nombre_tbl)
.HasForeignKey(e => e.idNombre)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Nombre_tbl>()
.HasMany(e => e.Medidas_tbl)
.WithRequired(e => e.Nombre_tbl)
.HasForeignKey(e => e.idNombre)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Pais_cat>()
.Property(e => e.Pais)
.IsUnicode(false);
modelBuilder.Entity<Pais_cat>()
.HasMany(e => e.Direccion_tbl)
.WithRequired(e => e.Pais_cat)
.HasForeignKey(e => e.idPais)
.WillCascadeOnDelete(false);
modelBuilder.Entity<database_firewall_rules>()
.Property(e => e.start_ip_address)
.IsUnicode(false);
modelBuilder.Entity<database_firewall_rules>()
.Property(e => e.end_ip_address)
.IsUnicode(false);
}
}
}