Accessing Rasor variables inside a foreach

Asked

Viewed 162 times

1

I am trying to list the contents of a table, showing in each item the items of another table that maintains a relationship with the first (1:N). For this I need 2 select, being the 2nd select based on each line of the result of the 1st select. The problem is that I don’t know how to identify, inside the SELECT string, the value brought by the RASOR relative to the id field.

@{
    string query = "SELECT id, tituloTopico FROM Topicos";

    var resultados = base_dados.Query(query);
    foreach (var linha in resultados)
    {       
        @linha.tituloTopico        
        string query1 = "SELECT tituloCatalogo FROM Catalogos WHERE Catalogos.id_Topico = @linha.id";
        var resultados1 = base_dados.Query(query1);
        foreach (var linha1 in resultados1)
        {
            @linha1.tituloCatalogo
        }
     }
 }

@line.id is not recognized within the Select statement. I am using cshtml and rasor page.

  • 2

    Don’t do this, this kind of processing should be on controller (assuming you’re using MVC.

1 answer

1

This type of logic should run on the Controller layer (whereas you are using MVC).

According to this article, the objective of Razor:

Razor appears in ASP.NET MVC 3, bringing the new concept of View Engine, which is nothing more than a template that implements a different syntax for the construction of web pages in ASP.NET, making the coding of web pages much simpler and more dynamic. Razor is an extra option for building web pages, further simplifying interface layer encoding. It is especially useful in ASP.NET MVC3 applications, where you search for clean and uniform code throughout the application.

Database queries and other logics must be implemented in the Controller.

Browser other questions tagged

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