I'm creating a basic ASP.NET Core MVC project. I already created the API project and I was able to login just fine with Swagger. So, now I want to create the web app project for the login page.
Here's the very basic login view:
@model MI_STOCK_SHOP.Models.LoginViewModel
<h2>Login</h2>
<form asp-action="Login" asp-controller="Account" method="post">
<div class="form-group mb-3">
<label asp-for="Username" class="form-label"></label>
<input asp-for="Username" class="form-control" />
<span asp-validation-for="Username" class="text-danger"></span>
</div>
<div class="form-group mb-3">
<label asp-for="Password" class="form-label"></label>
<input asp-for="Password" type="password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<button type="submit" class="btn btn-primary w-100">Login</button>
</form>
And my LoginViewModel.cs:
namespace MI_STOCK_SHOP.Models
{
public class LoginViewModel
{
public string Username { get; set; } = null!;
public string Password { get; set; } = null!;
}
}
The problem is Username and Password shows up as null in the console. So, it will always fail to login. When I inspect the page, input literally says asp-for=Username and asp-for=Password instead of automatically generating name=Username and id=Username in the rendered page.
I tested manually putting the name and id for the input fields, it works fine! I was able to login.
But of course, I wanted to use the asp-for instead of manually inserting the name and id. How do I fix this issue?