Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC submit button not firing

I am using ASP.net MVC 4 with the Razor engine.

I have a page (Index.cshtml) and a controller (HomeController.cs)

I am trying to hook up my submit button to an Action Result in my controller - however i can't seem to get it to fire.

My HTML :

@using (Html.BeginForm("SubmitForm", "Home", FormMethod.Post))
{
      <div class="main-Background">

      ******lots of other html here*******
        <button type="submit" id="btnSave">Save</button>

    </div>
}

My Controller :

public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }


        [HttpPost]
        public ActionResult SubmitForm()
        {
          return View();
        }

    }

At the moment i havn't implemented a model to pass the values through to the controller, i just wanted to see if i could get the ActionResult SubmitForm to fire.

I have tried @using (Html.BeginForm()) with no parameters, i have also tried including [HttpPost] above my ActionResult, without any luck.

Edit i have also tried using <input type="submit" id="btnSave">Save</input> instead of a button.

Not sure where i am going wrong

like image 540
DNKROZ Avatar asked Jun 26 '14 13:06

DNKROZ


1 Answers

It turns out that jQuery was stopping the ActionResult from being hit.

I had a button click event which was "eating up" the ActionResult functionality. I solved this by calling my ActionResult using Ajax.

like image 68
DNKROZ Avatar answered Oct 04 '22 14:10

DNKROZ