___ ___ erkimt Foreach in a detail ActionResult ______ qstntxt ___

I have a Registry entity that has a Teacher , the Teacher has a Record list. How can I create a foreach in the Details view of the Registry to list the Professor name that belongs to the record in question?

Controller registry:

%pre%

Record details view:

%pre%

Registration Class:

%pre%

Teacher class:

%pre%     
______ azszpr257196 ___

First, I believe your %code% class should contain %code% details. So, you just have to load the details in your action the following way:

%pre%

In View, you only have to add the %code% of the details.

%pre%     
___

1

I have a Registry entity that has a Teacher , the Teacher has a Record list. How can I create a foreach in the Details view of the Registry to list the Professor name that belongs to the record in question?

Controller registry:

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

Record 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; } 
}
    
asked by anonymous 21.11.2017 / 19:32

1 answer

1

First, I believe your RegistroCompleto class should contain ICollection details. So, you just have to load the details in your action the following way:

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

In View, you only have to 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>
    
21.11.2017 / 19:47