skip to Main Content

I have a partial view of users with a shop details page. I have added a partial modal to mark users as sickness. This is my partial view of the users:

@model Shop

<div class="mb-3 d-flex border-bottom align-items-center justify-content-between">
    <h3 class="text-secondary">Employees in @Model.Name</h3>

    <a [email protected]("admin") asp-controller="User" asp-action="Register" asp-route-id="@Model.Id"
        class="btn-add-employee" title="Add a new user"><i class="bi bi-person-fill-add" ></i></a>

</div>

<table class="table table-sm">
    <thead>
        <tr>
            <th>Name</th>
            <th>Surname</th>
            <th>Role</th>
            <th>Edit Clock In-Out and Add Holiday Leave</th>
            <th>Sickness</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @if (Model.Users.Count == 0)
        {
            <tr>
                <td colspan="4">No Employees...</td>
            </tr>
        }
        else
        {
            @foreach (var u in Model.Users)
            {
                
                var rowClass = "table-danger"; // Default to red

                 // Get today's timecard for the user
                 var todayTimeCard = u.TimeCards.Where(tc => tc.ClockIn.Date == DateTime.Today).OrderByDescending(tc => tc.ClockIn).FirstOrDefault();

                    if (todayTimeCard != null)
                    {
                        if (todayTimeCard.Holiday)
                        {
                            rowClass = "table-primary"; // Blue row for holiday
                        }
                        else if (todayTimeCard.Sickness)
                        {
                            rowClass = "table-warning"; // Yellow row for sickness
                        }
                        else
                        {
                            rowClass = "table-success"; // Green row for regular time card
                        }
                    }
              

                <tr class="@rowClass">
                    <td>@u.Name</td>
                    <td>@u.LastName</td>
                    <td>@u.Role</td>
                    <td>@u.ShopId</td>
                    <td> <a [email protected]("manager,admin") asp-controller="User" asp-action="TimeCardLog" asp-route-id="@u.Id"
                            class="btn"><i class="bi bi-pencil-fill"></i></a>
                    </td>
                    <td>
                          @{ViewBag.UserId = u.Id;}
                          <button [email protected]("manager,admin") data-bs-toggle="modal" data-bs-target="#AddSicknessTimeCardModal"
                    class="btn btn-sm btn-outline-warning"><i class="bi bi-emoji-frown" title="mark as sick"></i>Mark as Sick</button>
                    </td>

                    <td class="text-end">

                        <a [email protected]("admin") asp-controller="User" asp-action="Edit" asp-route-id="@u.Id"
                            class="btn btn-sm btn-outline-dark">Edit employee details</a>
                        <br>

                        <a [email protected]("admin") asp-controller="User" asp-action="Delete" asp-route-id="@u.Id"
                            class="buttondelete btn-sm btn-outline-danger">
                            <i class="bi bi-trash" title="Delete User"></i>Remove employee</a>
                        <br>

                        <a [email protected]("admin") asp-controller="User" asp-action="ReassignShop" asp-route-id="@u.Id"
                            class="buttondelete btn-sm btn-outline-danger">
                            <i class="bi bi-arrow-right" title="Reassign User"></i>Re-assign employee</a>

                    </td>
                   
                </tr>
            }
        }
    </tbody>
</table>



<!-- Mark as sickness Modal Confirmation -->
<partial name="_AddSicknessTimeCardModal" />

As you can see I tried using ViewBag to pass the userId but unfortunately it just stores the one id of the last user after iterating over them all. This is the modal for the add sickness leave:

<!-- Modal -->
<div class="modal fade" id="AddSicknessTimeCardModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h1 class="modal-title fs-5" id="exampleModalLabel">Mark Absence</h1>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div class="alert alert-warning">
                    <h3>Are you sure you want to mark this person as being sick today?</h3>
                </div>
                <div class="modal-footer">

                    <form asp-action="AddSicknessTimeCard" method="post" asp-route-id="@ViewBag.UserId">
                        <button type="submit" class="btn btn-primary" aria-label="add">Yes</button>
                        <button type="button" class="btn btn-danger" data-bs-dismiss="modal">Cancel</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

My controller needs the userid passed to it to complete the action (the u.Id from the foreach). Any suggestions for passing the id through to the modal partial would be appreciated!

2

Answers


  1. Chosen as BEST ANSWER

    So I came across making the model in my sickness modal an int and then using this model for the id.

     <@Html.Partial("_AddSicknessTimeCardModal", u.Id) /> 
    

    I changed the partial to an Html.Partial and was able to pass the id in as a model. This was added within my for each loop over the users.

    In my modal then I added a hidden input type into the form with the value of @Model and this worked.

    Not sure if there is a better way but I was just glad to get it working!!

    Came across this question in stackoverflow which helped

    is there a way that I can pass just an integer to my view without creating a model in mvc


  2. You should change

        <!-- Mark as sickness Modal Confirmation -->
        <partial name="_AddSicknessTimeCardModal" />
    

    to

    @foreach (var u in Model.Users)
    {
        ViewBag.UserId = u.Id;
        <!-- Mark as sickness Modal Confirmation -->
        <partial name="_AddSicknessTimeCardModal" />
    }
    

    and change your modal code from

    <!-- Modal -->
    <div class="modal fade" id="AddSicknessTimeCardModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog">
    ...
    

    to

    <!-- Modal -->
    <div class="modal fade" id="[email protected]" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog">
    ...
    

    and then you can show every user modal with this code :

    var userId = ...;
    $(`#AddSicknessTimeCardModal-${userId}`).modal('show');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search