我有一个表格如下:
<form action='@Url.Action("submitForm", "Home")'>
<fieldset>
<legend>User Details</legend>
<input type="checkbox" name="authorisedCheckbox" value="Authorised" id="authCheck" />Authorised<br />
<input type="checkbox" name="enabledCheckbox" value="Enabled" id="eCheck" />Enabled<br />
</fieldset>
<input type="submit" value="Save Changes" name="Save Changes">
<button type="button">Cancel</button>
</form>
这会调用 Controller 中的方法:
[HttpPost]
public ActionResult submitForm(string authorisedCheckbox, string enabledCheckbox)
{
System.Diagnostics.Debug.WriteLine("input values: ");
System.Diagnostics.Debug.WriteLine(authorisedCheckbox);
System.Diagnostics.Debug.WriteLine(enabledCheckbox);
//some other processing will be done here
return View();
}
但是当按下按钮时,表单会给出 404 未找到错误。我看过使用 @using (Html.BeginForm("method", "controller")) 的帖子 - 是使用 ASP.NET MVC 调用 Controller 方法的唯一方法吗?
最佳答案
感谢@Stephen Muecke 指出简单的事情就是将 method='post' 添加到标签中。
该操作还用于引用 View ,因此我将按照 @Dai 的建议将“submitForm”更改为更合适的内容。
关于javascript - ASP.Net MVC 提交表单找不到方法 - 无法使用普通表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43904484/