在Entity Framework中连接表并显示其他列

时间:2018-02-20 15:20:08

标签: asp.net-mvc entity-framework asp.net-identity

我试图让我的列表显示来自其他列的数据而不是其ID,目前它们显示如下:

enter image description here

这是我到目前为止所做的工作:

控制器:

public class CompanyAdmin
{
    public string Id { get; set; }
    [Required(AllowEmptyStrings = false)]
    [Display(Name = "Company Name")]
    public string CompanyName { get; set; }
    public virtual List<Certificate> Certificates { get; set; }


public class Certificate
{
    [Key]
    [Display(Name = "Certificate No.")]
    public string CertNo { get; set; }
    [Display(Name = "Company")]
    public IEnumerable<SelectListItem> Companies { get; set; }
    [Required]
    public string Company { get; set; }
    [Display(Name = "User")]
    public IEnumerable<SelectListItem> UserList { get; set; }
    [Required]
    public string User { get; set; }
    public string UploadedBy { get; set; }
    [Display(Name = "Description")]
    public string CertDescription { get; set; }
    [Display(Name = "File Location")]
    public string CertFileLocation { get; set; }
    [Display(Name = "Last Modified")]
    public DateTime LastModifiedDate { get; set; }

}

public class CertificateViewModel
{
    [Display(Name = "Certificate No.")]
    public string CertNo { get; set; }
    public string User { get; set; }
    [Display(Name = "User")]
    public string UserName { get; set; }
    public string Company { get; set; }
    [Display(Name = "Company")]
    public string CompanyName { get; set; }
    [Display(Name = "Description")]
    public string CertDescription { get; set; }
    [Display(Name = "Last Modified")]
    public DateTime LastModifiedDate { get; set; }
 }

这是我的控制器:

 public ActionResult Index()
    {
        if (User.IsInRole("Admin"))
        {
            IEnumerable<CertificateViewModel> model = null;

            model = (from c in db.CertificateDB
                     join u in db.Users on c.User equals u.Id
                     join d in db.CompanyDB on c.Company equals d.Id
                     select new CertificateViewModel
                     {
                         CertNo = c.CertNo,
                         UserName = u.UserName,
                         CompanyName = d.CompanyName,
                         CertDescription = c.CertDescription,
                         LastModifiedDate = c.LastModifiedDate
                     }
                );
            return View(model);
        }
        else
        {
            return RedirectToAction("Index", "Home");
        }

    }

和视图:

@model IEnumerable<IdentitySample.Models.CertificateViewModel>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.CertNo)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Company)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.User)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CertDescription)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.LastModifiedDate)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.CertNo)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Company)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.User)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CertDescription)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.LastModifiedDate)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.CertNo }) |
                @Html.ActionLink("Details", "Details", new { id = item.CertNo }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.CertNo })
            </td>
        </tr>
    }

</table>

我的背景:

 public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    static ApplicationDbContext()
    {
        // Set the database intializer which is run once during application start
        // This seeds the database with admin user credentials and admin role

        Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
    }

    public override int SaveChanges()
    {
        try
        {
            return base.SaveChanges();
        }
        catch (DbEntityValidationException ex)
        {
            string errorMessages = string.Join("; ", ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.PropertyName + ": " + x.ErrorMessage));
            throw new DbEntityValidationException(errorMessages);
        }
    }

    public DbSet<CompanyAdmin> CompanyDB { get; set; }
    public DbSet<Certificate> CertificateDB { get; set; }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

但是对于2个连接列,结果仍然是空的,请参见此处的屏幕截图enter image description here

知道我做错了吗?

2 个答案:

答案 0 :(得分:0)

在您的查询中填充属性CompanyName,但在您的视图中,您呈现属性Company。查询中的User与查询中的UserName相同。

另外,我建议在将模型传递给视图之前添加.ToList() - 这样可以确保在将数据传递到视图之前将数据从数据库中提取到对象中:

        model = (from c in db.CertificateDB
                 join u in db.Users on c.User equals u.Id
                 join d in db.CompanyDB on c.Company equals d.Id
                 select new CertificateViewModel
                 {
                     CertNo = c.CertNo,
                     UserName = u.UserName,
                     CompanyName = d.CompanyName,
                     CertDescription = c.CertDescription,
                     LastModifiedDate = c.LastModifiedDate
                 }
            ).ToList();

答案 1 :(得分:0)

您在控制器操作中为UserName设置了CompanyNameCertificateViewModel属性:

UserName = u.UserName,
CompanyName = d.CompanyName,

在浏览器中,您使用的是CompanyUser属性:

<td>
     @Html.DisplayFor(modelItem => item.Company)
</td>
<td>
     @Html.DisplayFor(modelItem => item.User)
</td>

因此,您需要在控制器中设置视图模型的CompanyUser属性,或者使用CompanyNameUserName在View中进行渲染,如果您更改了查看,更改它以使用正确的属性:

<td>
     @Html.DisplayFor(modelItem => item.CompanyName)
</td>
<td>
     @Html.DisplayFor(modelItem => item.UserName)
</td>