MVC提交按钮未触发
我正在使用带有Razor引擎的ASP.net MVC 4。
我有一个页面(Index.cshtml)和一个控制器(HomeController.cs)
我正在尝试将我的提交按钮连接到我的控制器中的操作结果 – 但是我似乎无法解决它。
我的HTML:
@using (Html.BeginForm("SubmitForm", "Home", FormMethod.Post)) { ******lots of other html here******* }
我的控制器:
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(); } }
目前我还没有实现模型将值传递给控制器,我只想看看是否可以触发ActionResult SubmitForm。
我试过没有参数的@using (Html.BeginForm())
,我也尝试在我的ActionResult上面包含[HttpPost]
,没有任何运气。
编辑我也尝试使用Save
而不是按钮。
不知道我哪里出错了
事实certificate,jQuery阻止了ActionResult被击中。
我有一个按钮点击事件,它正在“吞噬”ActionResultfunction。 我通过使用Ajax调用我的ActionResult解决了这个问题。
你不需要使用"-Controller"
后缀。 使用Home
而不是HomeController
,MVC会为你转换它。
使用
@using (Html.BeginForm("SubmitForm", "Home", FormMethod.Post, new { id = "submitForm" }))
代替
@using (Html.BeginForm("SubmitForm", "HomeController", FormMethod.Post, new { id = "submitForm" }))
完整代码
视图
@using (Html.BeginForm("SubmitForm", "Home", FormMethod.Post, new { id = "submitForm" })) { ******lots of other html here******* Save }
和控制器
[HttpPost] public ActionResult SubmitForm() { return View(); }
视图:
@using (Html.BeginForm("SubmitForm", "Home", FormMethod.Post)) { ******lots of other html here******* }
控制器:
[HttpPost] public ActionResult SubmitForm() { return View(); }
可能是因为你的div中的其他HTML发生了问题所以检查出来。 否则它完美无缺。
更改此@using (Html.BeginForm("SubmitForm", "HomeController", FormMethod.Post, new { id = "submitForm" }))
至
@using (Html.BeginForm("SubmitForm", "Home", FormMethod.Post, new { id = "submitForm" }))
说明:无需在任何地方后缀Controller,默认情况下接受
在控制器中
[HttpPost] public ActionResult SubmitForm(string id) { return View(); }
说明:由于您给出的表单方法是Post,因此需要在Action之前包含[HttpPost]
,并且在action方法中缺少您传递的参数