I have an HTML table in ASP.NET MVC that displays the data from the Model MyModel in an HTML table. Now what I need is for the Codigo_User and Codigo_Campo columns (which store codes) to show me the description of those codes, which are in other tables MyTablaCampos and MyTablaUsers Here I leave the definition of my models: MyModel
public partial class MyModel
{
public int Id { get; set; }
public string Codigo_User { get; set; }
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> Fecha_solicitud { get; set; }
public Nullable<int> Codigo_Campo { get; set; }
public string Ubicacion { get; set; }
public MyTablaCampos Campos { get; set; }
public MyTablaUsers Users { get; set; }
}
namespace MyProyect.Models
{
using System;
using System.Collections.Generic;
public partial class MyTablaUsers
{
public string Codigo_User { get; set; }
public string Nombre { get; set; }
public string Contacto { get; set; }
public string RFC { get; set; }
}
}
namespace MyProyect.Models
{
using System;
using System.Collections.Generic;
public partial class MyTablaCampos
{
public short Cod_Empresa { get; set; }
public string Codigo_User { get; set; }
public short Codigo_Campo { get; set; }
public string Descripcion { get; set; }
public short Tipo { get; set; }
public short Producto { get; set; }
}
}
This is the structure of my HTML table, in the columns where I want the names to appear, nothing comes out, so I don't know if there is another solution. Thanks.
@model IEnumerable<Inocuidad.Models.MyModel>
<div class="col-sm-12 table-responsive table-condensed table-sm" style="font-size:small">
<table class="table table-hover" id="dataTableCreditos">
@if (Model != null)
{
<thead class="thead-light">
<tr>
<th>Codigo_User</th>
<th>Usuario</th>
<th>Codigo_Campo</th>
<th>Campo</th>
<th>Fecha de solicitud</th>
<th>Ubicacion</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Codigo_User, new { id = "Codigo_User" })
</td>
<td>
@Html.DisplayFor(modelItem => item.Users.Nombre)
</td>
<td>
@Html.DisplayFor(modelItem => item.Codigo_Campo, new { id = "Codigo_Campo" })
</td>
<td>
@Html.DisplayFor(modelItem => item.Campos.Descripcion)
</td>
<td>
@Html.ValueFor(modelItem => item.Fecha_solicitud)
</td>
<td>
@Html.DisplayFor(modelItem => item.Ubicacion)
</td>
</tr>
}
</tbody>
}
</table>
</div>
public ActionResult SetSolicitud()
{
var item = from s in bd.MyModel select s;
return View(item.ToList());
}
Expected result
In principle, your code should work correctly, but there are a series of considerations that you do not indicate in your question, which may be the ones that are causing the error.
The first and most obvious is that the
MyTablaCampos
and propertiesMyTablaUsers
of the classMyModel
are not returning any value. Check this with the debugger before returning the results to the View from the Controller.The second option is that you are not returning the Model correctly from the Controller to the View. You should have stated your Controller code in the question. Note that your View must receive a Model of type
MyModel
, and you must indicate this with the directive@model List<MyModel>
. On the other hand, from the Controller you must send the Model with the data to represent to the View.I have reproduced your example in Visual Studio, and with the Controller that I indicate below it works correctly:
The problem has to be in the way the three tables in your example are related. The table
MyTablaCampos
also has a relationship with the tableMyTablaUsers
through the fieldpublic string Codigo_User { get; set; }
.When you perform the LinQ query
from s in bd.MyModel select s
, the condition that is translated to SQL would be:Try modifying the Model
MyTablaCampos
and adding the property to itpublic MyTablaUsers Users { get; set; }
:What you could do is create a ModelView to be able to execute what you are proposing.
In your controller:
I'll replace it with a lambda expression:
According to the official documentation :
Loading diligently (eagerly loading)
Eager loading is the process by which a query for one entity type also loads related entities as part of the query. Eager loading is achieved by using the include method. For example, the following queries will load blogs and all posts related to each blog.
Documentation note:
Include is an extension method on the namespace
System. Data. Entity
, so you need to make sure you're using that namespace.Comment:
Now in theory you already have the property loaded and it should be available to be rendered by the view.
Update:
In the class to which it is related:
Now regarding the fields:
To improve your understanding and mastery of the Entity Framework, this site is highly recommended: https://www.entityframeworktutorial.net/