Problem in View (details)

Asked

Viewed 56 times

0

I am repeating the Letterpress in the View, how can I use the ". first" in the query in the controller?

Error in the View: http://i.imgur.com/7t8AOJI.png? 1

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; }

    }
  • Um... if I understand correctly, then Tequilas is a list.. and you try to get 1 property of 1 object within the list... would not have to do a foreach?

1 answer

1

Young man, you can’t directly access a property on a list. Note that your view is powered by Ienumerable

@model IEnumerable<WebAppCatechesis2.ViewModels.GrupoViewModel>

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

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

The error will also happen 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>
  • If I had already figured that out, what I want is just a record. For example this is happening in the View. "Group Letter : A A A A A A" fiz isto no controlador e não funcionaLetterpress = g.LetraGrupo.first`.

  • To be more specific. Problem: http://i.imgur.com/7t8AOJI.png? 1

Browser other questions tagged

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