0

I would like to join 2 tables in MVC. After I created ViewModel, the View doesn't get any value from controller. I set a break point to see, and in the query value is null. There should be something wrong when parsing the data.

My Model:

 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; }
}

My Controller:

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);
    }

For My view, I imported @model IEnumerable<Research.ViewModels.ProgramViewModel> In the very begining. then

    @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)
Stanley
  • 87
  • 1
  • 8
  • 1
    Because you query returns a collection of anonymous objects, not a collection of `Program`. Create a view model, project your query to that view model, and then use that view model in the view. –  Jan 15 '18 at 09:48
  • Thanks for your help @StephenMuecke, is there any method without a viewmodel? – Stanley Jan 16 '18 at 00:48
  • Not if you want to do it correctly (and why would you even consider not using a view model?) –  Jan 16 '18 at 00:50
  • @StephenMuecke Last time I tried but it didn't work. Now seems like this is the only way. Anyway, thanks. – Stanley Jan 16 '18 at 01:11
  • @StephenMuecke I updated with a ViewModel. Could you please have a look at my problem now? – Stanley Jan 16 '18 at 05:51
  • What is your problem now? (and you do not need to select to an anonymous object then select to your view model -all you need to to select to the view model). And view models do not contains EF attributes such as `[Key]` –  Jan 16 '18 at 05:56
  • @StephenMuecke My view does not have any data from controller now. What do you mean to select to the view model? Is it like `from p in ViewModel` ? – Stanley Jan 16 '18 at 06:21
  • No, I mean all you need is `var query = (from p in program .... select new ProgramViewModel{ ProgramID = p.ProgramID, ...., Abbreviation = a.Abbreviation });` - you do not need to create a collection of anonymous objects first. –  Jan 16 '18 at 06:24
  • @StephenMuecke I changed my controller inthe post. But in `Model` of my `foreach` in the View, the `Count=0` and without showing any value in my view :( Sorry for wasting your time. – Stanley Jan 16 '18 at 06:55
  • Then you query is wrong of it does not return any records (but I do not know what the data in your tables is) –  Jan 16 '18 at 07:28

0 Answers0