Error running a Foreach ASP.NET MVC5

0

Hello Stack colleagues, I currently want to run some data through a cycle foreach but I have a problem always throws me the following error

  

Parser Error

     

Description: An error occurred during the parsing of a resource   required to service this request. Please review the following specific   parse error details and modify your source file appropriately.

     

Parser Error Message: The foreach block is missing a closing "}"   character Make sure you have a matching "}" character for all the   "{" characters within this block, and that none of the "}" characters   are being interpreted as markup.

part of my controller:

// GET: CalificarColaboradors
        public ActionResult Index(string id)
        {
            var calificado = (from p in db.CalificarColaboradors
                              where p.codigo_colaborador == id
                              select p.codigo_objetivos).ToList();

            var codigo = calificado[0];

            ViewBag.listobjetivos = (from p in db.Objectives
                                     where p.Codigo_Objetivos == codigo
                                     select p).ToList();

            ViewBag.calificacion = (from p in db.CalificarColaboradors
                                    where p.codigo_objetivos == codigo
                                    select p).ToList();

            ViewBag.fecha_actual = DateTime.Now.ToString("yyyy-MM-dd");
            ViewBag.Data_Collaborator = (from p in db.Collaborators
                                         where p.codigo == id
                                         select p).ToList();

            return View();
        }

Part of my view:

<tbody>
<tr style="background-color: rgba(112, 183, 50, 0.35);">
    <th>Objetivos</th>
    <th>Peso: 20%</th>
    <th>Descripción</th>
    <th>Calificación</th>
    <th>Observación</th>
</tr>
@foreach (var i in ViewBag.listobjetivos)
    {
        <tr>
            <td>@i.Nombre_Objetivo</td>
            <td>@i.Peso_Objetivo%</td>
            <td>@i.Descripcion_Objetivo</td>
    }

@foreach (var o in ViewBag.calificacion)
    {
            <td>@o.calificacion</td>
            <td>@o.observacion</td>
        </tr>
    }


</tbody>

and an image of what is seen by the error:

the error is solved when in the first foreach @foreach (var i in ViewBag.listobjetivos) it closes but it is not the idea, because I need both foreach to be a line.

Any questions or questions remain attentive.

  

UPDATE

They tell me that I can do a join in the query to nest them or put them together, but I honestly do not know how the procedure is done. My tables are these

I tried to do it in the following way and I got 16 results, and at this moment they would be 4.

ViewBag.test = (from p in db.Objectives
                     join pm in db.CalificarColaboradors on p.Codigo_Objetivos
                     equals pm.codigo_objetivos
                         where pm.codigo_objetivos == codigo
                     select new { Post = p, Meta = pm }).ToList();
    
asked by ByGroxD 16.06.2017 в 01:09
source

1 answer

1

The point is that you do not need to have two objects in the ViewBag , you can really achieve it by means of a single object doing join to the tables that you wish to consult. Another thing is that since we are going to create an object from a query of two tables, in the view you can not access an anonymous type, so we will create a model called ObjetivesCollabotaros which will serve as an interaction between the Controller and the View:

public class ObjetivesCollabotaros
{
    public string Nombre_Objetivo { get; set; }
    public decimal Peso_Objetivo { get; set; }
    public string Descripcion_Objetivo { get; set; }
    public decimal calificacion { get; set; }
    public string observacion { get; set; }
}

The data types I suppose are those, if they become others, you just have to adjust them to the corresponding ones.

Although the tables do not have an implicit relation in the database, they can be related through the field Objectives.id and CalificarColaboradors .id_objetivo . Viewing your table structure, the query should look something like this:

ViewBag.listobjetivos = (from p in db.Objectives
                        join o in db.CalificarColaboradors on p.id equals o.id_objetivo
                        where p.Codigo_Objetivos == codigo
                        && o.codigo_colaborador == id
                        select new ObjetivesCollabotaros {
                            Nombre_Objetivo = p.Nombre_Objetivo,
                            Peso_Objetivo = p.Peso_Objetivo,
                            Descripcion_Objetivo = p.Descripcion_Objetivo,
                            calificacion = o.calificacion,
                            observacion = o.observacion
                        }).ToList();

In the part of the view, you only need to iterate ViewBag.listobjetivos once:

@foreach (var i in ViewBag.listobjetivos)
{
    <tr>
        <td>@i.Nombre_Objetivo</td>
        <td>@i.Peso_Objetivo%</td>
        <td>@i.Descripcion_Objetivo</td>
        <td>@i.calificacion</td>
        <td>@i.observacion</td>
    </tr>
}
    
answered by 20.06.2017 / 20:32
source