You must create a ViewModel . See Update below.
This is the model representing your page. Elements that you want to show in your view must exist in your ViewModel. You will populate the ViewModel in your controller and display them on the page.
I wrote an example page of a shopping website with categories on the left and Products in the center. Both objects will exist in different tables.
Example:
class MainPageViewModel {
In your controller:
public class HomeController : Controller {
Then, in your opinion, you correctly display it:
@model MainPageViewModel @{ ViewBag.Title = "MainPage"; } <div id="left-bar"> <ul> @foreach (var category in Model.Categories) { <li>@category</li> } </ul> </div> <div id="center-content"> <ul> @foreach (var product in Model.Products) { <li>@product.Name</li> <li>@product.Price..</li> ..... } </ul> </div>
Refresh
This is your comment in which you mentioned that your tables and database columns change regularly.
I canβt say for sure, but maybe you should not make such tables as everyday ones, maybe there is a better database design that you may have, or maybe RDBMS is not suitable for you, and you should look into NoSql database (e.g. MongoDB )
However, if you continue with the code above, I suggest putting it in your own data layer class.
Also see Dapper for its very subtle level of data access, which simply retrieves objects from the database using sql queries or stored procedures. (Exactly what you need) This is done and used in stackoverflow.
source share