I created 2 data model from existing database with ado net entity data model. These tables has relation with each other. First table is category table and second table is products. So every category has many product. I want to display in a view some column of category also products which same in same category. I confused for this simple structure. I am away from mvc for a while. I need to solve this problem. Please help.
-
try some [googling. .](https://www.google.com/search?newwindow=1&safe=active&ei=HptFXNqbFsX7vASHo6WgDA&q=master+detail+mvc+5+msdn&oq=master+detail+mvc+5+msdn&gs_l=psy-ab.3..35i39.31579.32312..33205...0.0..0.166.711.0j5......0....1..gws-wiz.......0i71j0i22i30.mJ-QNhcJLjg) , check examples, [this](https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/index) and [this](https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-a-more-complex-data-model-for-an-asp-net-mvc-application) – Irf Jan 21 '19 at 10:16
1 Answers
You need to use View Models. A view model is related to MV* architectural patterns and is used to pass data to/from a view. I'll try to explain it in simple terms. Let's consider you have two tables:
Employee
---------------------------------------
| Id | Name | Designation |
---------------------------------------
| 1 | John Doe | Software Engineer |
---------------------------------------
| 2 | John Smith | Test Engineer |
---------------------------------------
Salary
----------------------------------------
| Id | EmployeeId | EmployeeSalary |
----------------------------------------
| 1 | 1 | $10,000 |
----------------------------------------
| 2 | 2 | $10,000 |
----------------------------------------
Now to access these two tables in your code, you have created Data Entities using ORM and it will create two classes as:
Employee.cs
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Designation { get; set; }
}
Salary.cs
public class Salary
{
public int Id { get; set; }
public int EmployeeId { get; set; }
public long EmployeeSalary { get; set; }
}
Now to use these two models to represent meaningful data onto the UI, you can create a ViewModel with all the necessary data entities and pass it through the controller to the view:
EmployeeViewModel.cs
public class EmployeeViewModel
{
public Employee EmployeeDetail { get; set; }
public Salary EmployeeSalaryDetail { get; set; }
}
EmployeeController:
public class EmployeeController
{
public ActionResult Index()
{
var model = new EmployeeViewModel();
return View(model);
}
}
For a significant different between data entities and view models, check this; Is it possible to create view models by out of models created from database tables using entity framework DB first approach?

- 1,813
- 1
- 16
- 37
-
Thank you for respond. I created a view model as your described. Next I called in in my view like @model Project.Models.MyViewModel. Then I am trying to access data like @Html.DisplayFor(model => model.Employee.name). But I got nothing. Am I doing somethings wrong? – Erman Elmalı Jan 21 '19 at 11:15
-
It's not `@Html.DisplayFor(model => model.Employee.name)`, it should be `@Html.DisplayFor(model => model.EmployeeDetail.Name )` – Sahil Sharma Jan 21 '19 at 11:29