1

I have a form and a few fields I want to post. When the model is sent to the controller method, all parameters are ok except the Email Address field which is always NULL. I can't figure out what i'm doing wrong.

Screenshot of The models is here

Screenshot of the controller and the method + the null fields are here

And the form i'm submitting is here

And the project files are in my Repo

Notice, all fields are OK except Email Address and Password Confirmation!

[Key]
        public int Id { get; set; }

        [Required]
        [Display(Name = "First Name")]
        [MaxLength(20, ErrorMessage = "Firstname cannot be contain more tahn 20 characters")]
        public string FirstName { get; set; }

        [Required]
        [Display(Name = "Last Name")]
        [MaxLength(20, ErrorMessage = "Lastname cannot be contain more tahn 20 characters")]        
        public string LastName { get; set; }

        [Required]
        [EmailAddress]
        [Display(Name = "Email Address")]
        [MaxLength(40, ErrorMessage = "Email Address cannot contain more that 40 characters")]        
        public string EmailAddress { get; set; }

        [Required]
        [Display(Name = "Password")]
        [MinLength(8, ErrorMessage = "Password must be atleast 8 characters")]
        [MaxLength(16, ErrorMessage = "Password cannot contain more that 16 characters")]        
        public string Password { get; set; }

        [Required]
        [Display(Name = "Password Confirmations")]
        [MinLength(8, ErrorMessage = "Password must be atleast 8 characters")]
        [MaxLength(16, ErrorMessage = "Password cannot contain more that 16 characters")]
        public string PasswordConfirmation { get; set; }

Signup controller:

private readonly UserContext dbContext;

        public SignupController(UserContext _dbContext)
        {
            this.dbContext = _dbContext;
        }

        public IActionResult Index()
        {
            return View();
        }

        //[HttpPost]
        //[ValidateAntiForgeryToken]
        public IActionResult CreateUser(User user)
        {
            // Check model validation
            if (!ModelState.IsValid)
                ViewBag.message = "AAAAA";

            // Check duplication
            if (dbContext.users.Any(e => e.EmailAddress == user.EmailAddress))
                return ViewBag("Email address registered");


            dbContext.Add(user);
            dbContext.SaveChanges();
            ViewBag.message = "The user " + user.FirstName + " " + user.LastName + " is saved successfully";

            //return View();
            return Content("Hi");
@model DataAccessLibrary.Models.User;
@{
    Layout = "_Layout";
    ViewBag.Title = "Sign up";
}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Login Page - Product Admin Template</title>
</head>

<body>

    <div class="container tm-mt-big tm-mb-big">
        <div class="row">
            <div class="col-12 mx-auto tm-login-col">
                <div class="tm-bg-primary-dark tm-block tm-block-h-auto">
                    <div class="row">
                        <div class="col-12 text-center">
                            <h2 class="tm-block-title mb-4">Create your account</h2>
                        </div>
                    </div>
                    <div class="row mt-2">
                        <div class="col-12">
                            <form id="signup_form" name="signup_form" asp-action="CreateUser" asp-controller="Signup" method="post" class="tm-login-form">

                                <!-- Firstname - Start -->
                                <div class="form-group inline-input-container">
                                    <label for="firstname">First Name</label>
                                    <input name="firstname" type="text" class="form-control validate" maxlength="20" id="firstname" value="" asp-for="FirstName" required />
                                    <span asp-validation-for="FirstName" class="text-danger"></span>
                                </div>
                                <!-- Firstname - End -->
                                <!-- Lastname - Start -->
                                <div class="form-group inline-input-container">
                                    <label for="lastname">Last Name</label>
                                    <input name="lastname" type="text" class="form-control validate" maxlength="20" id="lastname" value="" asp-for="LastName" required />
                                    <span asp-validation-for="LastName" class="text-danger"></span>
                                </div>
                                <!-- Lastname - End -->
                                <!-- Email - Start -->
                                <div class="form-group">
                                    <label for="email">Email Address</label>
                                    <input name="email" type="text" class="form-control validate" id="email" maxlength="40" value="" asp-for="EmailAddress" required />
                                    <span asp-validation-for="EmailAddress" class="text-danger"></span>
                                </div>
                                <!-- Email - End -->
                                <!-- Password - Start -->
                                <div class="form-group mt-3 inline-input-container">
                                    <label for="password">Password</label>
                                    <input name="password" type="password" class="form-control validate" maxlength="16" id="password" value="" asp-for="Password" required />
                                    <span asp-validation-for="Password" class="text-danger"></span>
                                </div>
                                <!-- Password - End -->
                                <!-- Confirm Password - Start -->
                                <div class="form-group mt-3 inline-input-container">
                                    <label for="confirm_password">Confirm Password</label>
                                    <input name="confirm_password" type="password" maxlength="16" class="form-control validate" id="confirm_password" asp-for="PasswordConfirmation" value="" required />
                                </div>
                                <!-- Confirm Password - End -->
                                <div class="form-group mt-4">
                                    <button id="submission" type="submit" class="btn btn-primary btn-block text-uppercase">Sign up</button>
                                </div>
                                <b id="msg"></b>
                            </form>
                            <div class="form-group mt-4">
                                <a asp-action="Index" asp-controller="Home" class="btn btn-primary btn-block text-uppercase inline" style="width: 60%">Resend Confirmation?</a>
                                <a asp-controller="Home" asp-action="Login" class="btn btn-primary btn-block text-uppercase inline" style="width: 40%">Login</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

@section Scripts{
    <script type="text/javascript">        
        //$('#submission').click(function () {
        //    var form = $("#signup_form");
        //    var url = form.attr("action");
        //    var formData = form.serialize();
        //    $.post(url, formData, function (data) {
        //        $("#msg").html(data);
        //    });
        //})
    </script>
}
7
  • 2
    The code needs to be in your question. Please ensure you thoroughly read How to Ask and related help topics in order to correctly set your expectations, and to avoid disappointment using Stack Overflow. Commented Sep 30, 2020 at 8:31
  • Note also that images of code are unacceptable, primarily because we can't debug an image. Commented Sep 30, 2020 at 8:32
  • best guess at present is that the password confirmation is blank and the email is blank so yeah, that will fail. What did you want to happen? Commented Sep 30, 2020 at 8:36
  • @John, sorry, I added the code as well Commented Sep 30, 2020 at 8:38
  • @RichardHousham, I added 'alert()' right before sending the request and both fields had values Commented Sep 30, 2020 at 8:39

1 Answer 1

2

you are overriding the name of the propery by using 'id' and 'name' elements. So the 'asp-for' is not used to render the property correctly. Use for the binding to work:

<input type="text" class="form-control validate" maxlength="40" value="" asp-for="EmailAddress" required />

Have fun!

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

Comments

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.