In my ASP.NET Core MVC website, I have a registration form. When I fill all the fields and I press the "Create Account" button, I stay in the same page, and the URL changes from
https://localhost:44395/CriarConta/Register?area=CriarConta
to
https://localhost:44395/CriarConta/Register
My register form (called CriarConta
) has the method=post
.
My problem is that the form isn't saving the user's information in the database, and I would like to solve this problem.
In case you need, here's my CriarContaController
:
using Microsoft.AspNetCore.Mvc;
using SportMotos.Models;
namespace SportMotos.Controllers
{
public class CriarContaController : Controller
{
private readonly AppDbContext _context;
public CriarContaController(AppDbContext context)
{
_context = context;
}
// Shows the form (GET)
[HttpGet]
public IActionResult Register()
{
return View();
}
// Processess the form(POST)
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Register(Cliente model)
{
if (ModelState.IsValid)
{
// Adds a new user to the DB
model.DataCriacao = DateTime.Now;
_context.Clientes.Add(model);
await _context.SaveChangesAsync();
return RedirectToAction("Index", "Home");
}
return View(model);
}
}
}