1

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?

1 Answer 1

3

Make sure you have the Views/_ViewImports.cshtml file that contains

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

Reference: @addTagHelper makes Tag Helpers available

Sign up to request clarification or add additional context in comments.

5 Comments

oh now it works. I wonder if this _ViewImports.cshtml has to be created manually or its actually supposed to be auto generated? because im making the project from scratch. I literally just picked new ASP NET CORE Web App project on visual studio
it wasnt there at first so i created it manually
As I know, this Views/_ViewImports.cshtml file is created automatically when creating the ASP.NET Core MVC project. Which ASP.NET Core version you are using?
8.0 I just noticed something. Theres folder called Pages. Inside of it is all you'd expect to be automatically generated inside Views folder. Maybe I picked the wrong project template from the beginning?
For Pages folder, more likely you are creating the project with ASP.NET Core Web App (Razor Pages), it should generate the _ViewImports.cshtml under the Pages folder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.