I have the following two tables (basic outline):
Tbl_CategoryType
ID LevelID Description
Tbl_Levels ID Name
Basically, I want to present all of the information in the Tbl_CategoryType table while referencing the Tbl_Levels.Name data based on the Tbl_CategoryType.LevelID number.
I have tried using a join in my repository as below;
public IQueryable GetAllTypesInCategory(int CatID)
{
return (from x in DBEntities.LU_LST_CategoryTypeSet
where x.CategoryID == CatID && x.Enabled == 1
join y in DBEntities.LU_LST_LevelSet on x.LevelID equals y.ID
select new {x, y});
}
However, when I call that method there is no type I can assign it to as it doesn't fit into the type of either the Category or Level.
I'm assuming I need to do this through a custom viewmodel but can't figure out the steps.
Thanks in advance
Inside the View, first you will need to import the namespace for accessing the Model classes. Then you will need to declare the Model for the View as dynamic. For displaying the records, two HTML Tables are used and by iterating over the Generic List Collection of Model objects, rows are added to the HTML Tables.
There are many ways to display data from more than one table. You can join tables or views by a common column. You can also merge data from two or more tables or views into a single column or create a subquery to retrieve data from several tables. You can use a SELECT statement to join columns in two or more tables.
ViewBag. ViewBag is a very well known way to pass the data from Controller to View & even View to View. ViewBag uses the dynamic feature that was added in C# 4.0. We can say ViewBag=ViewData + Dynamic wrapper around the ViewData dictionary.
By using this line in your linq statement:
select new {x, y}
you are creating a new anonymous type, which is a different type from your Entity types.
I'm guessing you're not using EntityFramework or some other heavy framework that will automatically resolve foreign key relationships to create linked entities. If true, then yes, you will need to create a ViewModel.
Just create a simple wrapper class that contains one of each entity as a property.
public class MyViewModel
{
public MyViewModel(LU_LST_CategoryTypeSet x, LU_LST_LevelSet y)
{
Category = x;
Level = y;
}
public LU_LST_CategoryTypeSet Category { get; set;}
public LU_LST_LevelSet Level { get; set; }
}
Then in your Linq statement, instead of creating anonymous types, create MyViewModel types:
public IQueryable GetAllTypesInCategory(int CatID)
{
return (from x in DBEntities.LU_LST_CategoryTypeSet
where x.CategoryID == CatID && x.Enabled == 1
join y in DBEntities.LU_LST_LevelSet on x.LevelID equals y.ID
select new {x, y});
}
Then copy the results into your model classes:
var listOfTypes = GetAllTypesInCategory(catID);
foreach (var item in listOfTypes)
{
var model = new MyViewModel(item.x, item.y);
//Do whatever with the model to get it to the view.
}
Make your View inherit from MyViewModel.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With