I can not solve one problem, because I have a bad understanding of ViewModel .

I have two models. This is a type of accounting unit.

 public class Ue_type { [Display(Name = "ID")] [Key] public int Id { set; get; } [Display(Name = "Тип")] [StringLength(200)] [Column(TypeName = "char")] public string Type_Name { set; get; } } 

And this is a unit of account model

 public class Ue_model { [Display(Name = "ID")] [Key] public int Id { set; get; } [Display(Name = "Модель УЕ")] [StringLength(200)] [Column(TypeName = "char")] public string Model_Name { set; get; } public int UetypeId { set; get; } } 

In the type we write for example Monitor, Laptop, Printer, etc. ... And in the model we describe the model Samsung S22 plus a type identifier 1.

So I want to bring this whole thing to View . In this form

 [__Модель____][__Тип____] [Samsung S22 ][ Монитор ] [Kyocera 102 ][ Принтер ] 

I made a controller

 public ActionResult Ue_Models() { var model_res = (from m in db.Ue_model join t in db.Ue_type on m.UetypeId equals t.Id select new { m.Model_Name, t.Type_Name }).ToList(); return View(model_res); } 

Correctly, I combined the two models using LINQ ? And how do I now bring all this to View . And if possible, give an example of a ViewModel I really want to look at ViewModel with this example.

    1 answer 1

    In order to display what you want in View , you need to describe a model that combines your two models.

    That is, you must describe the model, and in the controller, prepare the data and transfer it to the view.


    Create Model:

     public class DoubleModel { // код модели...(пример) public string ModelName { get; set; } public string ModelType { get; set; } public DoubleModel() { } } 

    In the view, we describe our model, which we will wait for:

     @model IEnumerable<DoubleModel> // код представления...(пример) @if (Model.Any()) { <table> <thead> <tr> <th>Модель</th> <th>Тип</th> </tr> </thead> <tbody> foreach (var ue in Model) { <tr> <td>@ue.ModelName</td> <td>@ue.ModelType</td> </tr> } </tbody> </table> } 

    The controller will give us the necessary data:

     public ActionResult Ue_Models() { var model = (from m in db.Ue_model join t in db.Ue_type on m.UetypeId equals t.Id select new DoubleModel { ModelName = m.Model_Name, ModelType = t.Type_Name }).ToList(); // подготовка данных на основе Вашего кода (пример) return View(model); } 

    The above code is illustrated as an example; it is necessary to adapt the given code example to your code. The idea itself should be understandable.