Hide @ Html.ActionLink in @foraech with Razor

0

My problem is that I want to hide the @Html.ActionLink from this view:

<table class="table">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.Subject.Name)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.State.Description)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Subject.Exams.FirstOrDefault().StartDate)
    </th>
    <th></th>
</tr>
@foreach (var item in Model.Subjects) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Subject.Name)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.State.Description)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Subject.Exams.FirstOrDefault().StartDate)
    </td>
    <td>
        @if ()
        {
            @Html.ActionLink("Inscribirse", "Register", new { id = item.StudentSubjectId })
        }

    </td>
</tr>

}

That has the model:

namespace Academica.Models.StudentExamViews
{
[NotMapped]
public class SearchView : StudentSubject
{
    public List<StudentSubject> Subjects { get; set; }
}
}

And the controller the next action:

public ActionResult Index()
    {
        var student = db.Students.Where(u => u.UserName == User.Identity.Name).FirstOrDefault();
        var subjects = db.StudentSubjects.Include(ss => ss.Subject.Exams).Where(ss => ss.StudentCareer.Student.StudentId == student.StudentId &&
                        ss.ExamEnabled == true).ToList();

        var view = new SearchView
        {
            Subjects = subjects,
        };

        return View(view);
    }

and there is the BD:  What I need is to hide the @Html.ActionLink "Register" when I have a record in the StudentExams table with the same StudentSubjectId that corresponds to each foreach item With the register button the records are saved in the StudentExams table with its respective StudentSubjectId (from the controller's other actions)

    
asked by Nicolas Comaschi 03.04.2018 в 03:03
source

1 answer

1

The first thing I would do is add the StudentExams include, that is, the line

.Include(ss => ss.Subject.Exams.StudentExams)

staying this way

var subjects = db.StudentSubjects
                    .Include(ss => ss.Subject.Exams)
                    .Include(ss => ss.Subject.Exams.StudentExams)
                .Where(ss => ss.StudentCareer.Student.StudentId == student.StudentId &&
                ss.ExamEnabled == true)
                .ToList();

later I do not understand why the foreach you do of the subjects and not of the exam directly

@foreach (var item in Model.Subject.Exams) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.StartDate)
        </td>
        <td>
            @if (!item.StudentExams.Any())
            {
                @Html.ActionLink("Inscribirse", "Register", new { id = item.Subject.StudentSubjectId })
            }

        </td>
    </tr>

}

in this way you could ask if there are any StudentExams related, if not there is a link

    
answered by 03.04.2018 / 08:20
source