Home > OS >  ASP.NET MVC form does not send data to controller
ASP.NET MVC form does not send data to controller

Time:12-12

I have an Index.cshtml page which displays a generic login page using Bootstrap. The markup looks like this:

@model Accurecord_Direct.Models.Login.  
@{
    ViewData["Title"] = "Home Page";
    Layout = "~/Views/Shared/_NoMenu.cshtml";
}
<body>
@using (Html.BeginForm("LogIn", "Home", FormMethod.Post))
{
<section  style="background-color: #508bfc;">
    <div >
        <div >
            <div >
                <div  style="border-radius: 1rem;">
                    <div >

                        <h3 >Log in</h3>

                        <div >
                            <input type="text" id="UserID"  />
                            <label  for="UserID">User ID</label>
                        </div>

                        <div >
                            <input type="password" id="Password"  />
                            <label  for="Password">Password</label>
                        </div>

                        <button  type="submit">Login</button>

                        <hr >

                        <button  style="background-color: #dd4b39;" type="submit" asp-action="ForgotUserID"><i ></i> Forgot User ID</button>
                        <button  style="background-color: #3b5998;" type="submit" asp-action="ForgotPassword"><i ></i>Forgot Password</button>

                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
}
</body>

My model looks like this:

using System;  

namespace Accurecord_Direct.Models
{
    public class Login
    {
        public string UserID { get; set; }
        public string Password { get; set; }
    }
}

Any my controller looks like this:

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

    [HttpPost]
    public ActionResult LogIn(IFormCollection form, [FromForm] Login newLogin)
    {
        return RedirectToAction("Index");
    }

    [HttpPost]
    [Route("ForgotUserID")]
    public IActionResult ForgotUserID()
    {
        return View("ForgotUserID");
    }

    [HttpPost]
    [Route("ForgotPassword")]
    public IActionResult ForgotPassword()
    {
        return View("ForgotPassword");
    }

I run my application with a breakpoint set at the return in the LogIn action method in the controller. When the breakpoint is hit, I look at the watch window for the variable newLogin. When I expand the variable, I see the two properties, UserID and Password, but they both have a value of (null). I am entering letters in the form prior to hitting the submit button.

Why wouldn't the form data get passed to the controller?

Thank you!

CodePudding user response:

fix the action

    [HttpPost]
    public ActionResult LogIn (Login newLogin)
    {
        return RedirectToAction("Index");
    }

and add asp-for for a view inputs

<div >
  <input type="text" asp-for="UserId" />
<label  for="UserID">User ID</label>
</div>
 <div >
<input type="password" asp-for=Password   />
 <label  for="Password">Password</label>
 </div>
  • Related