Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone!
I am trying to get Data from Multiple Tables in Database using ViewModel in ASP.NET MVC. But I always Get Null Value When I try to retrieve data Database table has Two Records Please see Below the Code and tell me how I can do it,

My ViewModel is

XML
public class LMSViewModel
{
    [Key]
    public int LMSViewModelId { get; set; }
    public List<Student> Students { get; set; }
    public List<Department> Departments { get; set; }
    public List<Teacher> Teahcers { get; set; }
    public List<Course> Courses { get; set; }
}


MY Controller is
C#
        public ActionResult Index()
        {
///LMSViewModel is my ViewModel Name
            LMSViewModel Container = new LMSViewModel();
            var DepteData = (from D in Db.Departments
                             select new 
                             {
                                 Name=D.DepartmentName,
                                 Budget=D.DepartmentBudget,
                                 Building=D.BuildingName
                             }).ToList();
//I also got error Here Error name is CS0029(Implicit Conversion)
            //Container.Students =DepteData;
            return View(DepteData);
        }


Please tell me how to show multitable in View Using .I am waiting your Reply Thank
Posted
Updated 15-Nov-15 13:43pm
v2

You need to select the particular object-
C#
public ActionResult Index()
{
	///LMSViewModel is my ViewModel Name
	LMSViewModel Container = new LMSViewModel();
	var DepteData = (from D in Db.Departments
					 select new Department
					 {
						 Name=D.DepartmentName,
						 Budget=D.DepartmentBudget,
						 Building=D.BuildingName
					 }).ToList();
					 
	Container.Departments = DepteData;

        // same way query for the Students, Teachers, Courses and assign them to Container
	return View(Container);
}


-KR
 
Share this answer
 
See below implementation how to pass multiple result set to View page from Controller:

Controller
C#
public ActionResult Index()
{
	///LMSViewModel is my ViewModel Name
	LMSViewModel Container = new LMSViewModel();
	var DepteData = (from D in Db.Departments
					 select new 
					 {
						 Name=D.DepartmentName,
						 Budget=D.DepartmentBudget,
						 Building=D.BuildingName
					 }).ToList();
					 
	//I also got error Here Error name is CS0029(Implicit Conversion)
	Container.Students =DepteData;
	// Assign other property like teachers, departments, id and others to Container object.
	return View(Container);
}

In Controller we assign only Students object - you need to assign other objects like departments, teachers. So that you can access those property in view page.

View
HTML
@model MultipleModelDemo.ViewModel.LMSViewModel 

@foreach (var item in Model.Students)
{         
        <div>@item.PropertyName</div>
}

@foreach (var item in Model.Departments)
{         
        <div>@item.PropertyName</div>
}

In view page, you need to change property names and ViewModel reference as per your need.

Moreover please visit Multiple Models in a View in ASP.NET MVC 4 / MVC 5[^]
 
Share this answer
 
v2
Comments
Member 11004573 16-Nov-15 1:13am    
Thank you for Helping but I am still got null value but I have 3 department in the database. I Create these table in in Applicationuser using Identity Customization please help me if you have any idea about it Thank you
[no name] 16-Nov-15 1:18am    
Firstly debug the code in side controller that you are getting 3 departments.
Secondly once you are getting correct value in Controller then assign that Department property to Container object.
Thirdly you need to set your peroprties in view properly otherwise it will give NULL value.
Member 11004573 16-Nov-15 1:33am    
yes I set the Debug at the Query but it return me null value But Database have 3 departments , please tell me another solution if possible
[no name] 16-Nov-15 1:43am    
Then there is problem with your query writing. Logically if it is not fetching any records from DB in controller then how it will display in View. Just rewrite your LINQ or SQL query properly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900