No related field is shown in a DataTable using C #

0

Suppose I have a class called Solicitud and inside I have the following attributes:

int id
int Nro
string Tipo
IList<Cuentas> cuentas

Note: accounts are linked by%% of request%

What I want to do is call a field belonging to class id and show it in Cuenta :

Something like this:

<tbody>
    @foreach (var item in Model)
    {
        <tr role="row">
            <td class="col-xs-1 col-sm-1 col-md-1 col-lg-1">
                @Html.DisplayFor(modelItem => item.id)
            </td>
            <td class="col-xs-2 col-sm-2 col-md-2 col-lg-2">
                @Html.DisplayFor(modelItem => item.Nro)
            </td>
            <td class="col-xs-2 col-sm-2 col-md-2 col-lg-2">
                @Html.DisplayFor(modelItem => item.Cuentas.Select(x => x.Descripcion)) ????
                @Html.DisplayFor(modelItem => item.Cuentas[0].Descripcion) ????
            </td>
        </tr>
    }
</tbody>

The syntax that I wrote does not know if it's the correct one because at the moment I'm not with Visual Studio at hand. I did it a little bit of memory. But what I want to reflect is the idea of what I want to do.  When I execute the project I get an error that only allows me to show fields, properties, etc. I think it has to do with what DataTable is a Cuentas . But when I load an object in the list of requests in the Controller , when I go through the records of is to list. For each record I have: List , id , nro , or the list of tipo . That only brings me a single record since it is related to the Cuentas of Request. And there I can see the field id .

But when I want to do the same in the View it does not recognize me. It does not leave me Descripcion . It only accepts simple attributes to show.

The question is, how can I show in a DataTable a field that is inside a DataTable object that is part of the List object that passes to View ?

    
asked by glthebest 10.02.2017 в 00:47
source

1 answer

0

Based on the fact that the definition of Model is a List<Solicitudes> , we can adjust the code in this way:

<tbody>
    @foreach (var item in Model)
    {
        <tr role="row">
            <td class="col-xs-1 col-sm-1 col-md-1 col-lg-1">
                @Html.DisplayFor(modelItem => item.id)
            </td>
            <td class="col-xs-2 col-sm-2 col-md-2 col-lg-2">
                @Html.DisplayFor(modelItem => item.Nro)
            </td>
            <td class="col-xs-2 col-sm-2 col-md-2 col-lg-2">
                foreach (var cuenta in item.cuentas)
                {
                    @Html.DisplayFor(modelItem => cuenta.Descripcion)
                }
            </td>
        </tr>
    }
</tbody>

Where now, to show all the accounts associated with each request you must iterate the element cuentas of the model with the following code section that is similarly found in the example above:

<td class="col-xs-2 col-sm-2 col-md-2 col-lg-2">
    foreach (var cuenta in item.cuentas)
    {
        @Html.DisplayFor(modelItem => cuenta.Descripcion)
    }
</td>
    
answered by 10.02.2017 в 01:20