显示3个表数据值

时间:2015-06-17 01:56:18

标签: c# asp.net-mvc

我的MSSQL数据库中有三个表。我已经创建了三个类文件。我需要做什么MVC C#index.chtml文件显示所有数据结果报告。在我的这3个表类下面只显示Region Name但不显示deliveryDay.DeliveryDay。它应该在周一,周五任何一天显示。谁能告诉我如何展示白天?

public class DeliveryTime
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int DeliveryAuto { get; set; }
    [Required]
    public int DeliveryTimeId { get; set; }
    [Required]
    public int DeliveryPeriodID { get; set; }
    [Required]
    [Display(Name = "Delivery Day:")]
    public string DeliveryDay { get; set; }
}

public class SubRegion
{   
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    [Key]
    public int SubregionID { get; set; }
    [Required]
    [Display(Name = "Sub Region Name:")]
    public string SubregionName { get; set; }
    // [Display(Name = "Region Name:")]
    public int RegionID { get; set; }
    [Display(Name = "Region Name:")]
    public virtual Region ReName { get; set; }
    [Required]
    public int DeliveryTimeId { get; set; }
    [Display(Name = "Delivery Day(s):")]
    public virtual DeliveryTime deliveryDay { get; set; } 
    public virtual IEnumerable<DeliveryTime> deliveryTime { get; set; }
    public virtual IEnumerable<Region> region { get; set; }
}

public class Region
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int RegionID { get; set; }
    [Required]
    [Display(Name = "Region Name:")]
    public string ReName { get; set; }
    public virtual IEnumerable<SubRegion> subRegion { get; set; }
  }

这是我的控制器

public ActionResult Index(int? page) 
{       
    var tLCSubRegion = db.TLCSubRegion.Include(s => s.ReName).ToList();
    if (Request.HttpMethod != "GET")
    {
        page = 1;
    }
    int pageSize = 20;
    int pageNumber = (page ?? 1);
    return View(tLCSubRegion.ToPagedList(pageNumber, pageSize));  
}

这是我的Index.chtml页面

@model PagedList.IPagedList<WebVer5.Models.SubRegion>
@foreach (var item in Model)
{
    @Html.DisplayFor(modelItem => item.ReName.ReName)
    @Html.DisplayFor(modelItem => item.deliveryDay.DeliveryDay) 
}

1 个答案:

答案 0 :(得分:1)

目前您只是加载区域。将控制器操作中的行更改为

var tLCSubRegion = db.TLCSubRegion.Include(s => s.ReName).Include(x => x.deliveryDay).ToList();

来自

var tLCSubRegion = db.TLCSubRegion.Include(s => s.ReName).ToList();