two tables in an Asp.NET MVC view [closed]

0

Good morning classmates a newbie question in ASP.NET MVC to put data from two different tables, do I need to create a third class so that the data can be seen in a single view? If you could give me an example in code or tutorial I would be very grateful.

    
asked by senseilex 10.08.2017 в 15:33
source

1 answer

0

In the MVC architecture pattern, the 'Model' part refers to the representation of information / data that makes up the system, and usually in asp .net mvc it is a correspondence of classes with the base tables of data. That is why the concept of ViewModel classes has been created, which are specific classes to display information / data that only belong to the view.

So yes, your approach to solving the problem is correct. That third class that you are thinking about creating would be your ViewModel. And it's as simple as:

ViewModel

namespace WebApplication3.ViewModels
{
    public class ViewModel1
    {
        public Table1 table1 { get; set; }
        public Table2 table2 { get; set; }
    }
}

Controller

public ActionResult Index()
{
    ViewModel1 viewModel1 = new ViewModel1();
    return View(viewModel1);
}

View

@model WebApplication3.ViewModels.ViewModel1
    
answered by 10.08.2017 / 18:19
source