In this example they are using ASP.NET MVC, C#, Razor and the code download link is a Visual Studio 2010 project.
So I decided to redo the example using Visual Studio 2013 and MVC 5.
Example 1
My Visual Studio 2013 app directory looks like this:
This is my code:
Controller
Model
MyModels.cs
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace SampleApp.Models{ public class Teacher { public int TeacherId { get; set; } public string Code { get; set; } public string Name { get; set; } } public class Student { public int StudentId { get; set; } public string Code { get; set; } public string Name { get; set; } public string EnrollmentNo { get; set; } } public class ViewModel { public IEnumerable<Teacher> Teachers { get; set; } public IEnumerable<Student> Students { get; set; } }}
If you notice in the code above, this example is using a static list of teachers and students which are declared in the controller.
So when I run my application above code it runs just fine and it looks like this:
So now I wanted to create another similar example using this same View Model technique.
Example 2
In example 2 instead of using a static list of teachers and students likes was done in Example 1
I am going to instead use the Employees table from the Northwind Sql Server database and the Department table of the AdventureWorks Sql Server database.
So my directory looks like this:
Northwind Employees table looks like this:
Adventureworks Department table looks like this:
My model for Northwind.edmx looks like this:
My model for Adventureworks.edmx looks like this:
Model
MyModel.cs
using System;using System.Collections.Generic;using System.Linq;using System.Web;using NorthwindApp.Models;namespace NorthwindApp.Models{ public class ViewModel { // Northwind - Employees public IEnumerable<Employee> Employees { get; set; } // AdventureWorks - Department public IEnumerable<Department> Departments { get; set; } }}
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using NorthwindApp.Models;namespace NorthwindApp.Controllers{ public class HomeController : Controller { // Multiple Model in single view using View Model // private NorthwindEntities db1 = new NorthwindEntities(); // GET: Home public ActionResult Index() { ViewBag.Message = "Welcome to my demo!"; ViewModel mymodel = new ViewModel(); // Northwind - Employees // create a list of teachers and pass it to model //mymodel.Teachers = GetTeachers(); //db1.Customers.ToList(); mymodel.Employees.ToList(); // AdventureWorks - Department // create a list of students and pass it to model //mymodel.Students = GetStudents(); //db1.Department.ToList(); mymodel.Departments.ToList(); return View(mymodel); } }}
When I run my page though I'm getting this error on my controller.
I think I'm not passing my Employe List and Department list correctly to my View.
Anyone know the correct syntax i should be using?
Normally when I want to pass a list from controller to my view i declare my entity like this
private NorthwindEntities db = new NorthwindEntities ();
Then in my actionresult i would pass one list like this.
// GET: Home
public ActionResult Index()
{
return View(db.Employees.ToList());
}
But in example 2 above i'm trying to pass 2 lists to my view.
Anyone know what i'm doing wrong?
ASP.NETC#
Last Comment
kaufmed
8/22/2022 - Mon
kaufmed
This has nothing to do with what you're passing. The problem is that, while you instantiated your ViewModel, you did not instantiate the Employees member that belongs to that class. Add a default constructor to your ViewModel, and instantiate the property there. Then your controller code should work fine.
I beg to differ. The gist of my post--which you even reiterated--was that the properties were not being instantiated. Whether that instantiation comes from a default constructor or the database, the property still has to be instantiated. Where the data comes from is an implementation detail.