Does not show a query data in the view

0

I'm working with C # ASP.NET MVC5, I'm new to this technology, I'm doing a web page where I need to display data from three tables in the view, I show them the structure of the database.

log table

|-----------------|-------------------|------------|
|  id_registro    |        2          |  3         |
|-----------------|-------------------|------------|
| fecha_registro  |     2017-05-21    | 2017-05-26 |
|-----------------|-------------------|------------|
| evento          | Aumento del valor | Disminuye  |
|-----------------|-------------------|------------|
|  id_pais        |       1           |   5

Table Country (n_pais)

| id_pais   |     1   |  5    |
|-----------|---------|-------|
|   pais    | Austria | India |
|-----------|---------|-------|
| id_region |   4     |   3   |

Region table (n_region)

| id_region | 3    |    4    |
|-----------|------|---------|
| region    | Mali | Venecia |

In the view what I want to show is:

Registro      Fecha        Evento             Pais      Region

  2           2017-05-21   Aumento del valor  Austria   Venecia
  3           2017-05-26   Disminuye          India     Mali 

The code in the view

<table class="table">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.id_registro
    </th>
    <th>
           @Html.DisplayNameFor(model => model.fecha_registro)
    </th>
    <th>
           @Html.DisplayNameFor(model => model.evento)
    </th>

    <th>
        @Html.DisplayNameFor(model => model.n_pais.pais)
    </th>
    <th>
       @Html.DisplayNameFor(model => model.n_region.region)
    </th>

</tr>
@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.id_registro)
           </td>
        <td>
            @Html.DisplayFor(modelItem => item.fecha_registro)
            </td>
        <td>
           @Html.DisplayFor(modelItem => item.evento)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.n_pais.pais)
        </td>
         <td>
            @Html.DisplayFor(modelItem => item.n_region.region)
        </td>
      </tr>
    }

</table>

The driver

The query in SQL

SELECT * FROM registro, n_pais, n_region
WHERE registro.id_pais = n_pais.id_pais AND
      n_pais.id_region = n_region.id_region

LinQ

var query = from q in db.registro
             join r in db.n_pais on q.id_pais equals r.id_pais
             join m in db.n_region on r.id_region equals m.id_region
             select q;
 return View(query.ToList());

In the view you can see the results of the registration and country table, however, the region remains empty. What can I do, what did I do wrong?

    
asked by Mary 26.12.2017 в 17:54
source

1 answer

1

Since Registration has no direct relationship with Region in this part you should change - >

@Html.DisplayFor(modelItem => item.n_region.region) by - > @Html.DisplayFor(modelItem => item.n_pais.n_region.region)

Although another possible solution to avoid such a large code in the view you can do it in the following way:

The class that you would have to add would be more or less like this:

public class DatosMostrar
{
    public int Registro { get; set; }
    public DateTime Fecha { get; set; }
    public string Evento { get; set; }
    public string Pais { get; set; }
    public string Region { get; set; }
}

to be able to assign you the data of the query that you have

and already the query in your method would be of the following way, assigning the values of the same one to the previously created class

 var query = from q in db.registro
             join r in db.n_pais on q.id_pais equals r.id_pais
             join m in db.n_region on r.id_region equals m.id_region
             select new DatosMostrar  
             {
              Registro =q.id_registro,
              Fecha = q.fecha_registro,
              Evento = q.evento 
              Pais = r.pais,
              Region  = m.region
             };

and already in the view you would change IEnumerable<TuProyecto.Models.registro> by IEnumerable<TuProyecto.Models.DatosMostrar>

and you would capture the data like this:

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Registro)
           </td>
        <td>
            @Html.DisplayFor(modelItem => item.Fecha)
            </td>
        <td>
           @Html.DisplayFor(modelItem => item.Evento)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Pais)
        </td>
         <td>
            @Html.DisplayFor(modelItem => item.Region)
        </td>
      </tr>
    }
    
answered by 26.12.2017 / 19:18
source