Foreach in an Actionresult of details

Asked

Viewed 81 times

1

I have an entity Registering who owns a Professor, the Professor has a list of Registering. How can I create a foreach in View details of Registering to list the name of Professor which belongs to the register in question ?

Registry controller:

public ActionResult Details(int? id)
{
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        RegistroCompleto registroCompleto = db.RegistrosCompletos.Find(id);
        if (registroCompleto == null)
        {
            return HttpNotFound();
        }
        return View(registroCompleto);
}

View log details view:

@model Meu.Projeto.Registro

<div>

    <dl class="dl-horizontal">

        <dt>
            @Html.DisplayNameFor(model => model.Professor.Nome)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Professor.Nome)
        </dd>


    </dl>
</div>
<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.IdRegistroCompleto }) 
    @Html.ActionLink("Back to List", "Index")
</p>

Registration Class:

public class Registro : RepositorioBase<Registro>
{
    [Key]
    [Display(Name = "Número do registro")]
    public int IdRegistroCompleto { get; set; }

    public Professor Professor { get; set; }

    [Display(Name = "Data de cadastro")]
    public DateTime DataCadastro { get; set; }
    public int IdProfessor { get; set; }
 }

Teacher class:

public class Professor : RepositorioBase<Professor>, IProfessor
{
     [Key]
     public int IdProfessor { get; set; } 

     public List<RegistroCompleto> RegistrosCompletos { get; set; } 
}
  • Put the classes involved?

  • Acebei to amend the Code

  • RegistroCompleto missed that ???

  • The class name was wrong, now it’s right

1 answer

1

First I believe your class RegistroCompleto should contain a ICollection details. All you have to do is load the details into your action as follows:

public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            RegistroCompleto registroCompleto = db.RegistrosCompletos.Include(a=>a.Detalhes).FirstOrDefault(a=>a.Id == id);
            if (registroCompleto == null)
            {
                return HttpNotFound();
            }
            return View(registroCompleto);
        }

Already in View just add the foreach of the details.

@model Meu.Projeto.RegistroCompleto

    <div>

        <dl class="dl-horizontal">

            <dt>
                @Html.DisplayNameFor(model => model.Professor.Nome)
            </dt>

            <dd>
                @Html.DisplayFor(model => model.Professor.Nome)
            </dd>


        </dl>
    </div>
@foreach(var detalhe in Model.Detalhes)
{
    <span>@detalhe.Propriedade</span>
}
    <p>
        @Html.ActionLink("Edit", "Edit", new { id = Model.IdRegistroCompleto }) 
        @Html.ActionLink("Back to List", "Index")
    </p>
  • Actually Details is not a class, just wanted to show the name of the teacher that belongs to the record in the View of the record

Browser other questions tagged

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