0

In my main View I have a foreach loop that loops through all my patients

<tbody>
                    @foreach (var patient in Model.Patients)
                    {
                        <tr>
                            <td>@Html.DisplayFor(modelItem => patient.FirstName)</td>
                            <td>@Html.DisplayFor(modelItem => patient.LastName)</td>
                            <td>@Html.DisplayFor(modelItem => patient.PersonalID)</td>
                            <td>
                                <a asp-action="Edit" asp-route-id="@patient.Id" class="btn btn-success"><i class="glyphicon glyphicon-pencil"></i>  Edit</a>
                                <a onclick="showDetails()" class="btn btn-success"><i class="glyphicon glyphicon-pencil"></i>  Detail</a>
                                <form asp-action="DeleteUser" asp-route-id="@patient.Id" method="post" style="display: inline;">
                                    <button class="btn btn-danger">
                                        Delete
                                    </button>
                                </form>
                            </td>
                        </tr>
                    }
                </tbody>

If I add another <div></div>

<div class="col-9 detailPatient" style="color:black;">
            @foreach (var doctor in Model.Doctors)
            {
                if (doctor.Id == patient.Id) <--------------- How do I get this patient.Id when someone clicks on the Details button. Do I need a partial View?
                {
                    <td>@Html.DisplayFor(modelItem => doctor.FirstName)</td>
                    <td>@Html.DisplayFor(modelItem => doctor.LastName)</td>
                    <td>@Html.DisplayFor(modelItem => doctor.GodineIskustva)</td>
                }
            }
        </div>

after the </tbody> tag, how can I when desplay item details that are associated with my @patient.Id?

JohnC
  • 171
  • 1
  • 1
  • 10
  • Does this answer your question? [How to render partial view with in Main view on link clicking in MVC..?](https://stackoverflow.com/questions/37754236/how-to-render-partial-view-with-in-main-view-on-link-clicking-in-mvc) – Selim Yildiz May 19 '20 at 11:40
  • @SelimYıldız Is there any chance you could show me how to implement that in my code? I've been trying the solution that you linked but it hasn't been working. – JohnC May 19 '20 at 12:33
  • I have added an answer and tried to explain how it works, hope it helps – Selim Yildiz May 19 '20 at 13:53

1 Answers1

1

You need to load details with using PartialView. Here is the full example of how it works:

First, you need to create new partial view:

@foreach (var doctor in Model.Doctors)
{
    <td>@Html.DisplayFor(modelItem => doctor.FirstName)</td>
    <td>@Html.DisplayFor(modelItem => doctor.LastName)</td>
    <td>@Html.DisplayFor(modelItem => doctor.GodineIskustva)</td>
}

In controller I assume you have method that retrieves doctors by patient:

// GET: Doctor/DetailsByPatientId/5
public ActionResult DetailsByPatientId(int id)
{
    List<Doctor> doctors = new List<Doctor>()
    {
        new Doctor(){ FirstName ="doctor 1"},
        new Doctor(){ FirstName ="doctor 2"}
    };
    return PartialView("_DoctorsByPatient", doctors);
}

And finally in your main Patient view, pass PatientId to showDetail function:

<a onclick="showDetails(@patient.PersonalID);" class="btn btn-success"><i class="glyphicon glyphicon-pencil"></i>  Detail</a>

Then load partial view with JQuery:

function showDetails(patientId) {
    $('.detailPatient').load("/Doctor/DetailsByPatientId/" + patientId);
}

See these references for more information:

Selim Yildiz
  • 5,254
  • 6
  • 18
  • 28
  • This works great! For anyone using this code in the future, make sure you're returning the right type of ViewModel from the controller. Thank you Selim! – JohnC May 19 '20 at 14:19