How to put an ID in a variable? ASP.NET MVC

Asked

Viewed 50 times

0

I currently have a view where a Table(Orders) is being loaded another table(Colors) I want each order to be a list of the color that has the same ID as the order.

My Tables:

public partial class Programa
{
    public int ID_Programa { get; set; }
    [System.ComponentModel.DataAnnotations.DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
    public Nullable<System.DateTime> Data_Registo { get; set; }
    public string Num_Encomenda { get; set; }
 }

public partial class Programa_Cor
{
    public int ID_Programa { get; set; }       
    public string Cor { get; set; }
}

To use both models in a single view I created this class

public class EncomendaViewModel
{
    public IEnumerable<Programa> Programas { get; set; }
    public IEnumerable<Programa_Cor> ProgramasCor { get; set; }
}

What seemed to me logical was to associate a variable to the Id_programa field and compare it with the Id_programa of the Programas_cor table, but all colors are listed on request and not only those that have the same ID.

My Controller:

var cor = color.Where(x => id.Contains(x.ID_Programa)).ToList()

        var tables = new EncomendaViewModel
        {
            Programas = embOpen.ToList(),
            ProgramasCor = cor.ToList(),
        };

I thank anyone who can help me!

---------------------Update-----------------------------

My View

<table class="table table-borderless table-sm " ;>
@foreach (var programa in Model.Programas)
{
    <tr style="border-top: 2px solid #cdd0d4;">

        <td style="width: 130px;">
            <b>Artigo: </b>@programa.Cod_Artigo

        </td>
        <td colspan="8">
            <b>Modelo: </b>@programa.Modelo
        </td>
    </tr>
    <tr>
        <td style="width: 200px;">
            <b>Nº Encomenda: </b>@programa.Num_Encomenda
        </td>
        <td style="width: 170px;">
            <b>Ref.Cliente: </b>@programa.Ref_Cliente)
        </td>          
    </tr>

    <tr>
        <td colspan="8">
            <table class="table table-bordered">
                @foreach (var programa_Cor in Model.ProgramasCor)
                {
                    <tr>
                        <td style="width: 150px;">
                            @programa_Cor.Cor)
                        </td>                        
                    </tr>
                }
            </table>
        </td>
    </tr>

}

view

At this time I have the colors that are loaded are only one id for testing as it carries same color in all Orders

  • Your modeling is a little strange and the question was not specific enough. However: I believe you have a problem in your modeling. As far as I understood, the correct thing would be to change the table from "Programacor" to only "Cor" and in the table "Programa" to put a foreign key referencing the table "Cor". Therefore, each program would have a color.

  • The way you look it doesn’t make sense to have a class Programa_Cor, only one property Cor in the program class

  • I’m going to put my view up to better understand my project

1 answer

0

Your problem is in domain modeling.

Some problems:

  1. You’re dealing with an N x N (Too Much) relationship, but you didn’t create one class / associative table to specify Order x Color relationship items.

  2. You are using nomenclatures that do not help in problem abstraction and are different from customer business (problem domain).

Let’s start by creating an associative class:

/// <summary>
/// Uma tabela associativa para guardar os itens do relacionamento N x N
/// </summary>
public class OrderColor
{
    public int Id { get; set; }
    public Color Color { get; set; }
    public int OrderId { get; set; }
    public int QtdPecas { get; set; }
    public string Talao { get; set; }
    public string Status { get; set; }
    public string Observacoes { get; set; }
}

Take the opportunity to improve entity nomenclatures for easier analysis. For example, instead of using the name Program, call of Order, and Color rather than Program_cor.

/// <summary>
/// Pedido
/// </summary>
public class Order
{
    public int Id { get; set; }
    [System.ComponentModel.DataAnnotations.DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
    public Nullable<System.DateTime> Data_Registo { get; set; }
    public string Num_Encomenda { get; set; }

    /// <summary>
    /// Todas as cores escolhidas
    /// </summary>
    public List<OrderColor> Colors { get; set; } = new List<OrderColor>();

    public void Add(Color color)
    {
        var corEscolhida = new OrderColor { OrderId = this.Id, Color = color };
        Colors.Add(corEscolhida);
    }
}

public class Color
{
    public int Id { get; set; }
    public string Nome { get; set; }

    public override string ToString()
    {
        return Nome;
    }
}

Your Viewmodel remains the same, I just added a comment to explain the differences of this property Colors and the property Colors of the Order class:

public class EncomendaViewModel
{
    public IEnumerable<Order> Encomendas { get; set; }
    /// <summary>
    /// Lista todas as cores disponíveis
    /// </summary>
    public IEnumerable<Color> Cores { get; set; }
}

To see what the usage looks like, I created an example implementation in Consoleapplication:

static void Main(string[] args)
{
    Console.WriteLine("TODAS AS CORES");

    // cria lista de cores
    var cores = new List<Color>();
    var branco = new Color { Id = 1, Nome = "BRANCO 500" };
    var lima = new Color { Id = 1, Nome = "LIMA 540" };
    var azul = new Color { Id = 1, Nome = "AZUL CLARO 560" };
    var taupe = new Color { Id = 1, Nome = "TAUPE 584" };

    cores.Add(branco);
    cores.Add(lima);
    cores.Add(azul);
    cores.Add(taupe);

    // lista na tela todas as cores disponíveis para serem escolhidas
    foreach (var c in cores)
    {
        Console.WriteLine(c);
    }

    Console.WriteLine("------------------------------------------------");
    Console.WriteLine("TODAS OS PEDIDOS");
    // cria as encomendas
    var encomendas = new List<Order>();
    var encomenda1 = new Order() { Id = 29798 };
    var encomenda2 = new Order() { Id = 29799 };

    encomenda1.Add(branco);
    encomenda1.Add(taupe);
    encomenda2.Add(lima);
    encomenda2.Add(azul);
    encomenda2.Add(taupe);

    encomendas.Add(encomenda1);
    encomendas.Add(encomenda2);

    // cria a viewmodel
    var model = new EncomendaViewModel
    {
        Encomendas = encomendas,
        Cores = cores
    };

    foreach (var o in model.Encomendas)
    {
        Console.WriteLine();
        Console.WriteLine($"PEDIDO - Artigo: {o.Id}");
        Console.WriteLine("CORES DO PEDIDO: ");

        foreach (var c in o.Colors)
        {
            Console.WriteLine($"Cor: {c.Color} | Observacoes: {c.Observacoes}");
        }
    }
}

Output:

TODAS AS CORES
BRANCO 500
LIMA 540
AZUL CLARO 560
TAUPE 584
------------------------------------------------
TODAS OS PEDIDOS

PEDIDO - Artigo: 29798
CORES DO PEDIDO:
Cor: BRANCO 500 | Observacoes:
Cor: TAUPE 584 | Observacoes:

PEDIDO - Artigo: 29799
CORES DO PEDIDO:
Cor: LIMA 540 | Observacoes:
Cor: AZUL CLARO 560 | Observacoes:
Cor: TAUPE 584 | Observacoes:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.