最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - ASP.NET Core MVC form not inserting data into database - Stack Overflow

programmeradmin1浏览0评论

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);
        }
    }
}
发布评论

评论列表(0)

  1. 暂无评论