MVC submit button does not work

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 connect my submit button to the result of an action in my controller, but I cannot get it to light up.

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 have not implemented a model for passing values ​​to the controller, I just wanted to see if I can run ActionResult SubmitForm.

I tried @using (Html.BeginForm()) without parameters, I also tried to include [HttpPost] over my ActionResult, with no luck.

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

I don't know where I'm wrong

+9
source share
5 answers

It turns out that jQuery was stopping the ActionResult action.

I had a button click event that "ate" the functionality of ActionResult. I solved this by calling my ActionResult using Ajax.

+12
source

You do not need to use the suffix "-Controller" . Use only Home instead of HomeController , MVC will convert it for you.

Using

 @using (Html.BeginForm("SubmitForm", "Home", FormMethod.Post, new { id = "submitForm" })) 

instead

 @using (Html.BeginForm("SubmitForm", "HomeController", FormMethod.Post, new { id = "submitForm" })) 

Full code

view

 @using (Html.BeginForm("SubmitForm", "Home", FormMethod.Post, new { id = "submitForm" })) { <div class="main-Background"> ******lots of other html here******* <input type="submit" id="btnSave">Save</input> </div> } 

And the controller

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

View:

 @using (Html.BeginForm("SubmitForm", "Home", FormMethod.Post)) { <div class="main-Background"> ******lots of other html here******* <button type="submit" id="btnSave">Save</button> </div> } 

Controller:

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

Perhaps the problem was due to another HTML inside your div, so check it out. Otherwise, it works fine.

+1
source

You need to add Html.BeginForm with parameters. Here is an example:

ActionName - The name of the action. In this case, the name is Create.

ControllerName - The name of the controller. In this case, the name of the house.

FormMethod - defines a form method, i.e. GET or POST. In this case, it will be set to POST.

 http://localhost:60386//Home/Create @using (Html.BeginForm("Create", "Home", FormMethod.Post)) { @Html.EditorFor(model => model.FirstName) <input type="submit" value="Create"/> } HomeController.cs: [HttpPost] public ActionResult Create(Person person) { if (ModelState.IsValid) { db.Persons.Add(person); db.SaveChanges(); return RedirectToAction("Create"); } return View(person); } 
0
source

Change this @using (Html.BeginForm("SubmitForm", "HomeController", FormMethod.Post, new { id = "submitForm" }))

to

@using (Html.BeginForm("SubmitForm", "Home", FormMethod.Post, new { id = "submitForm" }))

Explanation: There is no need for a controller suffix anywhere; it is accepted by default.

and in the controller

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

Explanation: since the form method you specified is a message, you must enable [HttpPost] before the action and the missing parameter are lost in the action method

-1
source

Source: https://habr.com/ru/post/971379/


All Articles