I am trying to follow a few tutorials on how to do this but I am coming out with no results.
I have to use two models in my view and they're both retrieving data from the database but when I try to combine them into one view it says I'm getting a null result. How can I query from the database and return both models to the view?
User.cs
public class User
{
[Key]
public int UserID { get; set; }
public string UserName { get; set; }
public string UserPassword { get; set; }
}
Course.cs
public class Course
{
[Key]
public int CourseID { get; set; }
public string CourseName { get; set; }
public string CourseDescription { get; set; }
}
CommonViewModel
public class CommonViewModel
{
public List<User> users { get; set; }
public List<Course> courses { get; set; }
}
DefaultConnection.cs
public class DefaultConnection : DbContext
{
public DefaultConnection() : base("DefaultConnection") { }
public DbSet<User> Users { get; set; }
public DbSet<Course> Vehicles { get; set; }
}
View
@model TEST.Models.CommonViewModel
@foreach (var item in Model.users)
{
@Html.DisplayFor(modelItem => item.UserID)
}
HomeController
private DefaultConnection db = new DefaultConnection();
public ActionResult Index()
{
List<User> users = new List<User>();
List<Course> courses = new List<Course>();
var viewModel = new CommonViewModel();
viewModel.users = users;
viewModel.courses = courses;
return View(viewModel);
}
CommonViewModel's properties should be public &List