CBreeze
CBreeze

Reputation: 2965

Controller action not being hit

I have the following form that is trying to let a User SignIn:

<form method="post" asp-controller="Auth" asp-action="SignIn">
    <div class="form-group row">
        <div class="col">
            <input class="form-control" type="email" placeholder="Email" id="email-input" name="email">
        </div>
    </div>
    <div class="form-group row">
        <div class="col">
            <input class="form-control" type="password" placeholder="Password" id="password-input" name="password">
        </div>
    </div>
    <div class="form-group row">
        <div class="col">
            <button class="btn btn-success" type="submit" name="button" style="width: 100%;">Login</button>
        </div>
    </div>
</form>

As you can see the form is using a Controller named Auth and an action on the Controller named SignIn. Here is a sample of the Controller:

public class AuthController : Controller
{
    public IActionResult Index()
    {
        return View(new SignInViewModel());
    }

    [HttpPost]
    public async Task<IActionResult> SignIn(SignInViewModel model)
    {
        if (ModelState.IsValid)
        {
            if (await _userService.ValidateCredentials(model.Email, model.Password, out var user))
            {
                await SignInUser(user.Email);
                return RedirectToAction("Index", "Home");
            }
        }
        return View(model);
    }
}

I've set a breakpoint on the line if (ModelState.IsValid) and it never gets hit. When I click the login button the page simply refreshes. I really cannot see why this breakpoint is not being hit when the Controller and the Action seem OK to me.

EDIT: SignInViewModel:

public class SignInViewModel
{
    [Required(ErrorMessage = "Please Enter an Email Address")]
    public string Email { get; set; }

    [Required(ErrorMessage = "Please Enter a Password")]
    public string Password { get; set; }
}

Snippet from Chrome:

enter image description here

Upvotes: 1

Views: 2095

Answers (1)

ManselD
ManselD

Reputation: 88

So I've made a new project in an attempt to replicate the issue and it works perfectly for me. I'm assuming you're not using Areas also? If you are using areas, add this attribute to your controller class:

[Route("AreaNameHere")]

Although if you are able to get to the index page then this isn't the issue.

Are your breakpoints valid? Are symbols being loaded? It could be refreshing due to the ModelState being invalid or the details being invalid and so it's returning the same view. You could use fiddler to see if it's actually making a request at all.

EDIT: Do you have this line "@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers" in your _ViewImports.cshtml file?

Upvotes: 1

Related Questions