0

I want learn EF 6 CodeFirst to an Existing Database (SQLServer)

When I trying to run my project i got this error:

Exception

System.ArgumentNullException: Value cannot be null. Parameter name: source

Controller

public ActionResult Index()
{
    var meetings = db.Meetings
        .OrderBy(e => e.StartDate)
        .Where(e => e.IsPublic)
        .Select(e => new MeetingViewModel()
            {
                MeetingId = e.MeetingId,
                MeetingName = e.MeetingName,
                MeetingTypeName = e.MeetingType.Name,
                LocationName = e.MeetingLocation.Name,
                StartDate = e.StartDate,
            });

    var upcomingMeetings = meetings.Where(e => e.StartDate > DateTime.Now);
    var passedMeetings = meetings.Where(e => e.StartDate <= DateTime.Now);
    return View(HomeIndex, new HomePageModel());
}

Page Model

public class HomePageModel
{
    public IEnumerable<MeetingViewModel> UpcommingMeetings { get; set; }
    public IEnumerable<MeetingViewModel> PassedMeetings { get; set; }
}

Cshtml

@model AquaEvent.Common.HomePageModel
<div class="row">
    @if (Model.UpcommingMeetings.Any())
    {
        @Html.DisplayFor(x => x.UpcommingMeetings)
    }
</div>

connectioString is EF AutoGenerator without changing

TheGeneral
  • 69,477
  • 8
  • 65
  • 107
hitasp
  • 461
  • 6
  • 18

1 Answers1

2

You are not passing the collections in the model and UpcommingMeetings causes null exception error in the view;

var upcomingMeetings = meetings.Where(e => e.StartDate > DateTime.Now);
var passedMeetings = meetings.Where(e => e.StartDate <= DateTime.Now);
return View(HomeIndex, new HomePageModel()
{
    UpcommingMeetings = upcomingMeetings,
    PassedMeetings = passedMeetings
});
lucky
  • 11,548
  • 4
  • 18
  • 35
  • Thank you, I lost 5h of my time for this, I looking for a error in my Db migration. because all of similar quiz linked me to it. Thank you! – hitasp Jan 03 '18 at 12:41