Problem in View (details)

0

I'm repeating the Letragroup in the View, how can I use the .first in the query in the controller?

View Error: link

View (details):

@model IEnumerable<WebAppCatechesis2.ViewModels.GrupoViewModel>
    <dl class="dl-horizontal">

                <dt>
                    Catequizandos:
                </dt>
                <table>
                    @foreach (var item in Model)
                    {
                        <tr>
                            <td>
                                @Html.DisplayFor(modelItem => item.NomeCatequizando)
                            </td>
                        </tr>
                    }
                </table>
                <dt>
                    LetraGrupo:
                </dt>
                @foreach (var item in Model)
                {
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => item.LetraGrupo)                               
                        </td>

                    </tr>
                }

            </dl>

Controller:

public ActionResult Detalhes(int? id)
        {
            var grupo = db.Grupo.Where(g => g.GrupoID ==id).FirstOrDefault();

            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            //query para recolher as nomes dos catequizandos pertencentes ao grupo selecionado
            var queryNomeCatequizandos = (from g in db.Grupo
                                          join i in db.Inscricao on g.GrupoID equals i.GrupoID
                                          join c in db.Catequizando on i.CatequizandoID equals c.CatequizandoID
                                          join p in db.Pessoa on c.CatequizandoID equals p.PessoaID
                                          where g.GrupoID == i.GrupoID 
                                          && i.CatequizandoID == c.CatequizandoID 
                                          && c.CatequizandoID == p.PessoaID 
                                          && g.GrupoID == id
                                          select new GrupoViewModel
                                          {
                                              GrupoID = i.GrupoID ?? 0,
                                              NomeCatequizando = p.Nome,
                                              LetraGrupo = g.LetraGrupo,
                                              Sala = g.Sala                                                                                  
                                          });

            if (queryNomeCatequizandos == null)
            {
                return HttpNotFound();
            }
            return View(queryNomeCatequizandos.ToList());
        }

ViewModel:

public class GrupoViewModel
    {
        public int GrupoID { get; set; }
        public String AnoPastoral { get; set; }
        public String HoraInicio { get; set; }
        public String DiaSessao { get; set; }
        public String AnoCatequese { get; set; }
        public String LetraGrupo { get; set; }
        public String Sala { get; set; }
        public String Observacoes { get; set; }
        public String NomeCatequizando { get; set; }

    }
    
asked by anonymous 07.01.2016 / 18:11

1 answer

1

Young, you can not directly access a property in a list. Note that your view has the IEnumerable

@model IEnumerable<WebAppCatechesis2.ViewModels.GrupoViewModel>

And just below you try to access a property from that list without specifying the element, you are accessing it as if it were just a GropoViewModel. The error is here:

@Html.DisplayNameFor(model => model.LetraGrupo)

The error will also occur on all lines below:

                    <dt>
                        @Html.DisplayFor(model => model.LetraGrupo)
                    </dt>
                    <dt>
                        @Html.DisplayNameFor(model => model.Sala)
                    </dt>
                    <dt>
                        @Html.DisplayFor(model => model.Sala)
                    </dt>

Edit your view and put it this way that should work:

@model IEnumerable<WebAppCatechesis2.ViewModels.GrupoViewModel>
   <div class="panel-body">
                <table>
                   @foreach (var grupo in Model)
                  {
                  <tr>
                    <td>
                        @Html.DisplayNameFor(model => grupo.LetraGrupo)
                    </td>
                    <td>
                        @Html.DisplayFor(model => grupo.LetraGrupo)
                    </td>
                    <td>
                        @Html.DisplayNameFor(model => grupo.Sala)
                    </td>
                    <td>
                        @Html.DisplayFor(model => grupo.Sala)
                    </td>

                    <td>
                        @Html.DisplayFor(model => grupo.NomeCatequizando)
                    </td>
                   </tr>

                   }
                 </table>  
            </div>
    
07.01.2016 / 22:13