How can i show data from two tables in my sql database in the view…?
Please help.
- table is Profile
- table is Company
I want two different tables in my view to show these data from database.!
I have searched in the internet but i couldn’t find anything to solve my problem. Please help.
I have no problem with one table in my view.
public ActionResult Profiler()
{
using (var dc = new Models.DataDataContext()) // Opretter objekt af model(Databasen)
{
return View(dc.Profils.OrderByDescending(a => a.Nu).ToList()); // Returner en liste med tabellets indhold
}
}
This give me a table with data from my database in my view. But i want my second table too.
What you’re looking to achieve is termed a “View Model” because the model you are sending to your views is different that the model in your database. Often times you will find that you may need to create a view for the UI that doesn’t exactly match the database.
jgauffin shows a “DetailsModel” which combines two database entities. This is as simple as creating a class and giving it two properties, CompanyName and Username. What his code is missing is the code for the “DetailsModel” class. Instead of passing two entities you create a single entity that has all of the properties you need.
Surjit also has a method to do such without the use of a seperate View Model class by using a Tuple.
You will need to modify your view to accept either the Tuple or your new View Model.