Im doing a dontnet website and am having issues with a method in my controller. This is the method in my user controller to allow users to view all their timecards. I tried logging to the terminal and its coming up 0.
// GET /User/TimeCardLog
public IActionResult TimeCardLog(int userId)
{
Console.WriteLine("The id is " + userId);
// Get the signed-in user's ID
var signedInUserId = User.GetSignedInUserId();
// Determine which user's timecards to retrieve
int targetUserId;
if (userId == signedInUserId)
{
targetUserId = signedInUserId;
}
else
{
targetUserId = userId;
}
var user = _svc.GetUser(targetUserId);
if (user is null)
{
Alert($"User {targetUserId} Not Found..", AlertType.warning);
return RedirectToAction(nameof(Index));//TimeCardLog
}
var timeCards = _tcSvc.GetTimeCardsByUser(targetUserId);
var model = new UserTimeCardsViewModel
{
User = user,
TimeCards = timeCards,
};
return View(model);
}
This is my partial view of users belonging to a shop. The edit, reassign and delete user all work for the user intended but when I click on edit timecards I’m getting user 0 not found (so my user is null). I’m now not able to get into the user log either from the personal profile (so the signedinuserid method is not working??)
@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>
</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 = "";
// 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)
{
rowClass = "table-success"; // Green row for users currently clocked in or a timecard for the current day
}
else
{
rowClass = "table-danger"; // Red row for users not currently clocked in or no timecard for that day
}
<tr class="@rowClass">
<td>@u.Name</td>
<td>@u.LastName</td>
<td>@u.Role</td>
<td>@u.Id</td>
<td> <a [email protected]("admin, manager") asp-controller="User" asp-action="TimeCardLog" asp-route-id="@u.Id"
class="btn"><i class="bi bi-pencil-fill"></a></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 to new shop
</a>
</td>
</tr>
}
}
</tbody>
</table>
I initially didn’t have the userId as a parameter but I have added that. I am wanting to see the timecardlog view page which i had seen when I was just using the signedinuser id previously.
I am very new to coding and this is my first real experience creating a website. It might be a simple solution but I can’t seem to figure this out. Any help or ideas would be apprecited!
2
Answers
I was passing my @u.Id and this has to be exactly the same as what is passed into the controller.
So userId must be written as id as this is what Razor is passing it as.
Not sure of the explanation if it has something to do with how Razor handles the parameters. If anyone has an explanation I'd love to know!
you can change your code from:
to:
goodluck.