locked
Request.Form always comes up empty RRS feed

  • Question

  • User1266884534 posted

    I have a simple form that submits to an actionResult method on a controller. The issue I am facing is that the Request.Form collection is always coming up empty on the other side.

    Here is my form.

    @using (Html.BeginForm("LoginUserActionResult", "Home", FormMethod.Post, new {id = "SignInForm"}))
    {
    <form class="form-signin" id="SignInForm">
    <img class="mb-4" src="" alt="" width="72" height="72">
    <h1 class="h3 mb-3 font-weight-normal">Please sign in</h1>
    <label for="inputEmail" class="sr-only">Email address</label>
    <input type="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
    <label for="inputPassword" class="sr-only">Password</label>
    <input type="password" id="inputPassword" class="form-control" placeholder="Password" required>
    <div class="checkbox mb-3">
    <label>
    <input type="checkbox" value="remember-me"> Remember me
    </label>
    </div>
    <span class="btn btn-lg btn-primary btn-block" type="submit">Sign in</span>
    <input type="submit" class="btn btn-lg btn-primary btn-block" value="Submit" />
    </form>
    }

    Here is my controller method.

    [HttpPost]
    public ActionResult LoginUserActionResult()
    {

    var inputEmail = Request.Form["inputEmail"];
    var inputPassword = Request.Form["inputPassword"];

    if (String.IsNullOrEmpty(inputEmail) || String.IsNullOrEmpty(inputPassword))
    {
    ViewBag.Message = "Enter Valid Email and Password in order to continue!";
    return View("Index");
    }

    Session["UserName"] = inputEmail;
    var wc = new WebClient();
    var response = wc.DownloadString($"http://localhost:1123/api/Login?username={inputEmail}&hashpassword={inputPassword}");
    ViewBag.LoginResponse = response;
    return View("PaymentHistory");
    }

    Thursday, August 1, 2019 7:44 PM

Answers

  • User-474980206 posted

    a browser html form submit on includes form elements (input, select, textarea) that have  a name attribute. they must also be enabled, and in the case of checkbox/radio must be checked. as none of your <input>'s have a name attribute, they will not be in the post data.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 1, 2019 7:55 PM

All replies

  • User-474980206 posted

    a browser html form submit on includes form elements (input, select, textarea) that have  a name attribute. they must also be enabled, and in the case of checkbox/radio must be checked. as none of your <input>'s have a name attribute, they will not be in the post data.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 1, 2019 7:55 PM
  • User1520731567 posted

    Hi learning_to_code,

    As @bruce said,you just add name attribute in your html tag,for example:

    <input type="email" id="inputEmail" name="inputEmail"class="form-control" placeholder="Email address" required autofocus>
    

    Controller get value from html tags' name,actually,you just could add the same name parameter,like:

    public ActionResult LoginUserActionResult(string inputEmail)//inputEmail parameter is the same with Request.Form["inputEmail"]
            {
                var inputEmail2 = Request.Form["inputEmail"];
                return View();
            }

    Best Regards.

    Yuki Tao

    Friday, August 2, 2019 2:45 AM