Chris Petraskie
Chris Petraskie

Reputation: 21

ASP.NET form not submitting

When I click submit, the page is refreshed and looks like everything went through, but is not hitting the ActionResult I am trying to send it to.

I've tried submitting normally and with JavaScript and neither will work.

<form method="post" autocomplete="off" asp-controller="Default" asp-action="Submit">
    <button id="SubmitButton" type="submit">Submit</button>
</form>

Controller Method:

namespace (Removed).Controllers
{
    public class DefaultController : Controller
    {
        [HttpPost]
        public ActionResult Submit()
        {
            DBController1 DB1 = new DBController1();
            AlertManagement am = new AlertManagement();

            am.FirstName = Request.Form["FirstName"];
            am.LastName = Request.Form["LastName"];
            am.Email = Request.Form["EmailAddress"];
            am.Mobile = Request.Form["PhoneNumber"].Replace("(", "").Replace(")", "").Replace(" ", "").Replace("-", "");
            am.Affiliation = Request.Form["Affiliation"];
            am.StartDate = Convert.ToDateTime(Request.Form["StartDate"]).Date;
            am.EndDate = Convert.ToDateTime(Request.Form["EndDate"]).Date;

            DB1.AlertManagement.Add(am);
            DB1.SaveChanges();

            return RedirectToAction("Index");
        }

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

Here is the RouteConfig:

namespace (Removed)
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Index",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "Submit",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Default", action = "Submit", id = UrlParameter.Optional }
            );
        }
    }
}

When I click submit, it always goes to ActionResult Index()

Upvotes: 1

Views: 155

Answers (2)

Shubham Singh
Shubham Singh

Reputation: 75

try this

<form action="/Default/Submit" method="post">
<input type="submit">
</form>

the ActionMethod you are expecting to be called is of Verb 'GET' while your Form submit method type is 'POST' add HttpPost attribute it shouls work

 [HttpPost]
Public ActionResult Submit()
{
}

Upvotes: 0

Bradley Petersen
Bradley Petersen

Reputation: 155

Taking a shot in the dark here, but does your controller look like this?

public class DefaultController: Controller
    {
        [HttpPost] // This attribute states that this action result can only be accessed when using http POST verb.
        public IActionResult Submit()
        {
            return RedirectToAction("index", "home");
        }

    }

Also, what are you posting? If you have intentionally left out the fields in your example make sure that in your POST method you have this.

<form method="post" autocomplete="off" asp-controller="Default" asp-action="Submit">
<input type="text" name="nameOfElement" id="clientName" />
    <button id="SubmitButton" type="submit">Submit</button>
</form>
    public class DefaultController: Controller
            {
                [HttpPost] 
                public IActionResult Submit([FromForm] string nameOfElement)
                {
                    return RedirectToAction("index", "home");
                }

            }

Upvotes: 1

Related Questions