MVC加入2个表时出错

时间:2018-01-15 09:44:15

标签: c# asp.net-mvc-4 model controller

我想在MVC中加入2个表。创建ViewModel后,View无法从控制器获取任何值。我设置了一个断点来查看,query值为null。解析数据时应该有问题。

我的模特:

 public class ProgramViewModel
{
    [Key]
    public string ProgramID { get; set; }
    public string ProgramName { get; set; }
    [DataType(DataType.Date)]
    public DateTime? DateReleased { get; set; }
    public string AwardAmount { get; set; }
    [DataType(DataType.Date)]
    public DateTime? SubmissionDeadline { get; set; }
    public string DepartmentName { get; set; }
    public string Abbreviation { get; set; }

    public Program ProgramVM;
    public Department DepartmentVM;
    public Agency AgencyVM;
}

[Table("RP_Department")]
public class Department
{
    [Key]
    public int DepartmentID { get; set; }
    public string DepartmentName { get; set; }
    public string ChineseName { get; set; }
    public int AgencyID { get; set; }
    public virtual ICollection<Program> Programs { get; set; }
}

    [Table("[RP_Program]")]
public class Program
{
    [Key]
    public string ProgramID { get; set; }
    public string ProgramName { get; set; }
    [DataType(DataType.Date)]
    public DateTime? SubmissionDeadline { get; set; }
    public int AgencyID { get; set; }
    public virtual Agency Agency { get; set; }
    [ForeignKey("FundingLevel")]
    public int LevelID { get; set; }
    public FundingLevel FundingLevel { get; set; }
    [ForeignKey("Department")]
    public int DepartmentID { get; set; }
    public  virtual Department Department { get; set; }
    [DataType(DataType.Date)]
    public DateTime? DateReleased { get; set; }


[Table("RP_Agency")]
public class Agency
{
    [Key]
    public int AgencyID { get; set; }
    public string Abbreviation { get; set; }
    public string Description { get; set; }
    public string ChineseName { get; set; }
    public virtual ICollection<Program> Programs { get; set; }
}

我的控制器:

public ActionResult All()
    {
    List<Program> program = new List<Program>();
    List<Department> departments = new List<Department>();
    List<Agency> agency = new List<Agency>();

        var query = (from p in program
                    join d in departments
                    on p.DepartmentID equals d.DepartmentID
                    join a in agency
                    on p.AgencyID equals a.AgencyID
                    orderby p.SubmissionDeadline descending
                    select new ProgramViewModel(){
            ProgramID = p.ProgramID,
            ProgramName = p.ProgramName,
            DateReleased = p.DateReleased,
            AwardAmount = p.AwardAmount,
            DepartmentName = d.DepartmentName,
            Abbreviation = a.Abbreviation
                });

        return View(query);
    }

对于我的观点,我导入了@model IEnumerable<Research.ViewModels.ProgramViewModel> 在一开始。 那么

    @foreach (var item in Model) {
    @Html.DisplayFor(modelItem =>@item.ProgramID)
    @Html.DisplayFor(modelItem => @item.ProgramName)
    @Html.DisplayFor(modelItem => @item.DateReleased)
    @Html.DisplayFor(modelItem => @item.AwardAmount)
    @Html.DisplayFor(modelItem => @item.SubmissionDeadline)
        @Html.DisplayFor(modelItem => @item.DepartmentName)

0 个答案:

没有答案