locked
Problem posting form RRS feed

  • Question

  • User-1204637165 posted

    There all this sound simple but it got me all thinking.

    I created a simple form and am suppose to post values to the controller but it just not working. I dont know what the problem is. Each time I submit the form just does not show sign that it redirecting or submitting to the view.

     [Bind("PromptID,Email,Password")]
        public class Setquery
        {
            [Required]
            public string PromptID { get; set; }
    
            [Required]
            public string Email { get; set; }
    
            [Required]
            [DataType(DataType.Password)]
            public string Password { get; set; }
         
        }
    
    
     <form asp-action="activate" asp-controller="home" method="post">
                <h4>Use a local account to log in.</h4>
                <hr />
             
    
                <div class="form-group">
                    <label  asp-for="PromptID" class="control-label ">Promptfinder Subdomain:</label>
    
                    <div class="input-group">
                        <div class="input-group-addon">http://</div>
                        <input asp-for="PromptID" class="form-control" placeholder="Username" aria_label="Username" aria_describedby="basic-addon1" required="required" />
                        <div class="input-group-addon"><b>@Backupserver</b></div>
                    </div>
    
                    <span asp-validation-for="PromptID" class="text-danger"></span>
                </div>
    
                <div class="form-group">
                    <label asp-for="Email"></label>
                    <input asp-for="Email" type="email" class="form-control"  required/>
                    <span asp-validation-for="Email" class="text-danger"></span>
                </div>
    
                <div class="form-group">
                    <label asp-for="Password"></label>
                    <input asp-for="Password" class="form-control" required />
                    <span asp-validation-for="Password" class="text-danger"></span>
                </div>
    
                <div class="form-group">
                    <button type="submit" class="btn btn-primary">Log in</button>
                    <button type="reset" class="btn btn-default">Reset</button>
                </div>
    
            </form>
    
    
    
    This is inside Homecontroller
    
      [HttpPost]
            [ValidateAntiForgeryToken]
            public Possetupobj Activate(Setquery model)
            {
                Response.WriteAsync(model.Email); return null;
    
    }
    
    Startup.cs
    
      app.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Home}/{action=Index}/{id?}");
                });

    Wednesday, July 10, 2019 5:51 PM

Answers

  • User475983607 posted

    You're returning a null type which results in a 204 (No Content) response.  The browser does not refresh the page.

    Fix the action to return content like Json if you're testing.

            [HttpPost]
            [ValidateAntiForgeryToken]
            public IActionResult Activate()
            {
                return Json(new { message = "This is a test" });
            }
    

    Edit: Remember to use the browse's dev tools (F12) when debugging,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 10, 2019 6:15 PM

All replies

  • User475983607 posted

    You're returning a null type which results in a 204 (No Content) response.  The browser does not refresh the page.

    Fix the action to return content like Json if you're testing.

            [HttpPost]
            [ValidateAntiForgeryToken]
            public IActionResult Activate()
            {
                return Json(new { message = "This is a test" });
            }
    

    Edit: Remember to use the browse's dev tools (F12) when debugging,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 10, 2019 6:15 PM
  • User-1204637165 posted

    Thanks I did f12. It was a javascript problem. There was a script throwing an error somewhere on the page.

    Wednesday, July 10, 2019 8:02 PM
  • User475983607 posted

    Thanks I did f12. It was a javascript problem. There was a script throwing an error somewhere on the page.

    Your response does not makes sense. 

    1) There's no JavaScript shown.  2) Returning a null type results in a 204 regardless of the JavaScript error which will not stop the submit.

    Wednesday, July 10, 2019 8:18 PM