Reputation: 45
asp-route does not seem to be sending an Id over to my View Method correctly, when using it within a tag. The id gets passed in as "null". When I use it with an a tag it works fine(except it doesn't open a modal); however, I want to make the whole row clickable to edit.
I tried to check casing to make sure it wasn't something silly; however, when paste the same code into an tag it has 0 issues. When I look in the console, I can see that asp-route-id is pulling the correct id. It hits the controller, and goes through the if(user == null) because it comes in as null.
HTML:
@model PortalDev.Models.ApplicationUser
<tr data-toggle="ajax-modal" data-target="#editUser" data-url="@Url.Action("EditUser")" asp-action="EditUser" asp-controller="Administration" asp-route-id="@Model.Id">
<td class="text-left">@Model.Id</td>
<td class="text-left">@Model.UserName</td>
<td class="text-left">@Model.Email>
<td class="text-left">@Model.PhoneNumber</td>
<td class="text-left">@Model.City</td>
<td>
<button type="button" id="btnOpen" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#SendEmail">
<i class="glyphicon glyphicon-envelope"></i>
</button>
<a asp-action="EditUser" asp-controller="Administration" asp-route-id="@Model.Id">Edit</a>
</td>
</tr>
Controller:
[HttpGet]
public async Task<IActionResult> EditUser(string id)
{
var user = await userManager.FindByIdAsync(id);
if (user == null)
{
ViewBag.ErrorMessage = $"User with Id = {id} cannot be found";
return View("NotFound");
}
var userClaims = await userManager.GetClaimsAsync(user);
var userRoles = await userManager.GetRolesAsync(user);
var model = new EditUserViewModel
{
Id = user.Id,
Email = user.Email,
UserName = user.UserName,
City = user.City,
Claims = userClaims.Select(c => c.Value).ToList(),
Roles = userRoles
};
return PartialView("~/Views/Modals/_EditUserModalPartial.cshtml", model);
}
Expected behavior is to click on the table row and it brings up a modal with the EditUserViewModel
Upvotes: 0
Views: 696
Reputation: 1361
Replace data-url="@Url.Action("EditUser")"
with data-url="@Url.Action("EditUser", new {Id = Model.Id})"
. This will generate correct Url.
Upvotes: 0