C
C#2mo ago
Cydo

✅ MVC application routing issue.

Why does the following code not work
<div>
<a asp-route-productId="@product.Id"
class="btn btn-primary bg-gradient border-0 form-control">
Details
</a>
</div>
<div>
<a asp-route-productId="@product.Id"
class="btn btn-primary bg-gradient border-0 form-control">
Details
</a>
</div>
But this code does
<div>
<a href="@Url.Action("Details", "Home", new { productId = product.Id })"
class="btn btn-primary bg-gradient border-0 form-control">
Details
</a>
</div>
<div>
<a href="@Url.Action("Details", "Home", new { productId = product.Id })"
class="btn btn-primary bg-gradient border-0 form-control">
Details
</a>
</div>
32 Replies
Angius
Angius2mo ago
Because you did not specify the route Only the parameter of that route You need at least asp-action to point to the action the link supposed to link to And maybe even asp-controller if the controller is not the current one The second piece of code contains all those details, so it works
Cydo
Cydo2mo ago
If you mean like so, the same issue persists.
<a
asp-controller="Home"
asp-action="Details"
asp-route-productId="@product.Id"
class="btn btn-primary bg-gradient border-0 form-control">
Details
</a>
<a
asp-controller="Home"
asp-action="Details"
asp-route-productId="@product.Id"
class="btn btn-primary bg-gradient border-0 form-control">
Details
</a>
Cydo
Cydo2mo ago
I dont even get syntax highlighting with this
No description
Angius
Angius2mo ago
Huh Show the signature of the controller and action? Also, what .NET version?
Cydo
Cydo2mo ago
.net 8.0 HomeController.cs
public IActionResult Details(int productId)
{
ShoppingCart cart = new()
{
Product = _unitOfWork.Product.GetFirstOrDefault(u => u.Id == productId, includeProperties: "Category"),
Count = 1,
ProductId = productId
};
return View(cart);
}

[HttpPost]
[Authorize]
public IActionResult Details(ShoppingCart shoppingCart)
{
Debug.WriteLine($"Count: {shoppingCart.Count}");


var claimsIdentity = (ClaimsIdentity)User.Identity;
var userId = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier).Value;
shoppingCart.ApplicationUserId = userId;

ShoppingCart cartFromDb = _unitOfWork.ShoppingCart.GetFirstOrDefault(u =>
u.ApplicationUserId == userId && u.ProductId == shoppingCart.ProductId);

if (cartFromDb != null)
{
cartFromDb.Count += shoppingCart.Count;
_unitOfWork.ShoppingCart.Update(cartFromDb);
}
else
{
_unitOfWork.ShoppingCart.Add(shoppingCart);
}



_unitOfWork.Save();

return RedirectToAction(nameof(Index));
}
public IActionResult Details(int productId)
{
ShoppingCart cart = new()
{
Product = _unitOfWork.Product.GetFirstOrDefault(u => u.Id == productId, includeProperties: "Category"),
Count = 1,
ProductId = productId
};
return View(cart);
}

[HttpPost]
[Authorize]
public IActionResult Details(ShoppingCart shoppingCart)
{
Debug.WriteLine($"Count: {shoppingCart.Count}");


var claimsIdentity = (ClaimsIdentity)User.Identity;
var userId = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier).Value;
shoppingCart.ApplicationUserId = userId;

ShoppingCart cartFromDb = _unitOfWork.ShoppingCart.GetFirstOrDefault(u =>
u.ApplicationUserId == userId && u.ProductId == shoppingCart.ProductId);

if (cartFromDb != null)
{
cartFromDb.Count += shoppingCart.Count;
_unitOfWork.ShoppingCart.Update(cartFromDb);
}
else
{
_unitOfWork.ShoppingCart.Add(shoppingCart);
}



_unitOfWork.Save();

return RedirectToAction(nameof(Index));
}
Angius
Angius2mo ago
public IActionResult Details(int productId)
I assume it's a [HttpGet] action?
Cydo
Cydo2mo ago
yes should i put the annotation aboive it regardless?
Angius
Angius2mo ago
Well, yeah? Attributes is how you do routing
Cydo
Cydo2mo ago
im following a course for .net mvc and he didnt do it, so i didnt do it lol
Angius
Angius2mo ago
If you don't tell ASP that it's a GET endpoint, how is it supposed to know?
Cydo
Cydo2mo ago
Even with that, it doesnt work the way it should
Angius
Angius2mo ago
huh What HTML gets generated?
Cydo
Cydo2mo ago
No description
Angius
Angius2mo ago
Well that's weird
Cydo
Cydo2mo ago
yeah lol
Want results from more Discord servers?
Add your server