locked
Form Not Posting to Controller RRS feed

  • Question

  • User-1958760912 posted

    I have a simple asp.net core app that I'm toying around with to learn the framework. 

    I'm trying to create a registration form for a website.  I know there are certain poor practices in the code (such as plain-text passwords) but that is not a huge concern right now as this is more of a practice app than anything else.

    My view is here:

    @{
        ViewBag.Title = "Register";
    }
    
    @model Person
    @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
    @addTagHelper *, AuthoringTagHelpers
    
    <h2>Register</h2>
    
    
    <form asp-action="Register" asp-controller="Person" method = post>
        <div class="form-group"><label asp-for="Handle"></label>
        <input asp-for="Handle"></div>
    
        <div class="form-group"><label asp-for="Name"></label>
        <input asp-for="Name"/></div>
    
        <div class="form-group"><label asp-for="Surname"></label>
        <input asp-for="Surname"/></div>
    
        <div class="form-group"><label asp-for="Password"></label>
        <input asp-for="Password"/></div>
    
        <div class="form-group"><label asp-for="Location"></label>
        <input asp-for="Location"></div>
    
        <button type="submit" class="btn btn-default">Submit</button>
    </form>
    

    My controller is here:

    using FND.Models;
    using FND.ContextDBs;
    using Microsoft.AspNetCore.Mvc;
    
    
    namespace FND.Controllers
    {
        public class RegistrationController : Controller
        {
            private RegistrationContext db = new RegistrationContext("Server=localhost; Port=3306; database=registrations; user=root; password=***");
    
            public IActionResult Index()
            {
                return View("Registration");
            }
    
            [HttpPost]
            public IActionResult Register(Person reg)
            {
                if (reg is null)
                {
                    throw new System.ArgumentNullException(nameof(reg));
                }
    
                db.Registrations.Add(reg);
                db.SaveChanges();
    
                return RedirectToAction("Home/Index");
            }
        }
    }

    The problem I'm having is that I know my form is communicating with my Model, because if I change something one of the fields like "Surname" to something that isn't in the model, I get an error.

    But, if I try to sign up a user not only does the database not update, but rather than Redirecting to my Home/Index view, I simply get the Registration page refreshed with a blank form.

    I feel like there is some small, dumb thing that I'm doing here, but I can't for the life of me see it.

    If there's any help that you can provide me, I would be so grateful.

    Thank you!

    Tuesday, July 30, 2019 6:40 PM

Answers

  • User475983607 posted

    The form POSTs to the the Person controller not the Registration controller.  Also post should be in double quotes.

    <form asp-action="Register" asp-controller="Person" method="post">
    public class RegistrationController : Controller
    {
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 30, 2019 6:46 PM

All replies

  • User475983607 posted

    The form POSTs to the the Person controller not the Registration controller.  Also post should be in double quotes.

    <form asp-action="Register" asp-controller="Person" method="post">
    public class RegistrationController : Controller
    {
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 30, 2019 6:46 PM
  • User-1958760912 posted

    That definitely fixed it.

    Thank you so much.

    Tuesday, July 30, 2019 6:56 PM