C
C#4mo 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
Angius4mo 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
CydoOP4mo 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
CydoOP4mo ago
I dont even get syntax highlighting with this
No description
Angius
Angius4mo ago
Huh Show the signature of the controller and action? Also, what .NET version?
Cydo
CydoOP4mo 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
Angius4mo ago
public IActionResult Details(int productId)
I assume it's a [HttpGet] action?
Cydo
CydoOP4mo ago
yes should i put the annotation aboive it regardless?
Angius
Angius4mo ago
Well, yeah? Attributes is how you do routing
Cydo
CydoOP4mo ago
im following a course for .net mvc and he didnt do it, so i didnt do it lol
Angius
Angius4mo ago
If you don't tell ASP that it's a GET endpoint, how is it supposed to know?
Cydo
CydoOP4mo ago
Even with that, it doesnt work the way it should
Angius
Angius4mo ago
huh What HTML gets generated?
Cydo
CydoOP4mo ago
No description
Angius
Angius4mo ago
Well that's weird
Cydo
CydoOP4mo ago
yeah lol
Angius
Angius4mo ago
Seems the tag helpers are not working
Cydo
CydoOP4mo ago
they are working in my other files tho idk why its just this one
Angius
Angius4mo ago
Is this view placed somewhere else than other views? What's your Views/_ViewImports.cshtml file?
Cydo
CydoOP4mo ago
@using BulkyWeb
@using Bulky.Models;
@using Bulky.Models.ViewModels;
@using Bulky.Utility
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@using BulkyWeb
@using Bulky.Models;
@using Bulky.Models.ViewModels;
@using Bulky.Utility
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
and its Areas/Customer/Views/Home/Index.cshtml
Angius
Angius4mo ago
And the views where tag helpers work are somewhere else? You'll need to set up separate viewimports per area
Cydo
CydoOP4mo ago
Areas/Admin/Views/ has them working
Cydo
CydoOP4mo ago
but they are setup like that
No description
Angius
Angius4mo ago
Right are the viewimports files the same for each? This line is important here:
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Cydo
CydoOP4mo ago
yup
Angius
Angius4mo ago
It's in both viewimports files?
Cydo
CydoOP4mo ago
yeah
Angius
Angius4mo ago
Well, I'm at a loss then Oh Actually Try adding asp-area as well?
Cydo
CydoOP4mo ago
still nothing ill keep bashing my head against the issue lol, been trying to solve it since early this morning xD I decided to just manually retype all this and save...
@using BulkyWeb
@using Bulky.Models
@using Bulky.Models.ViewModels
@using Bulky.Utility
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@using BulkyWeb
@using Bulky.Models
@using Bulky.Models.ViewModels
@using Bulky.Utility
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
and now it works...
Cydo
CydoOP4mo ago
syntax highlighting too
No description
Angius
Angius4mo ago
Good ol' "turn it off and back on again" never fails lmao
Cydo
CydoOP4mo ago
hopefully this fixes my issue on why my add to cart page, saves 0 to my database no matter what number i put in the input as well yes, the tag helpers were the issue all the time, thanks for helping me so much, been at this since 9am lol, and its now 5pm xD
Angius
Angius4mo ago
Anytime :Ok:
Want results from more Discord servers?
Add your server