[英]How do I post back to an MVC controller action via Javascript
Currently I have a aspx page that is defined as:目前我有一个 aspx 页面,定义为:
<%Using Html.BeginForm("SaveNotifications", "Notifications", FormMethod.Post, New With {.id = "NotificationForm"})%>
...
<tr>
<td colspan="3">
<input type="submit" id="SaveNotifications" name="SaveNotifications" value="Save" class="t-button" />
</td>
</tr>
</table>
</fieldset>
<%End Using %>
I want to change this so that my input button fires a javascript function so that I can do some complex validation of the form and THEN post to the controller.我想更改它,以便我的输入按钮触发 javascript function 以便我可以对表单进行一些复杂的验证,然后发布到 controller。
I changed my submit button to:我将提交按钮更改为:
<input type="submit" id="SaveNotifications" name="SaveNotifications" value="Save" class="t-button" onclick="onSave(); return false;" />
The onSave() method would fire but not post back to the controller. onSave() 方法会触发但不会回发到 controller。
The controller is defined as: controller 定义为:
<HttpPost()> _
<MultiButton(MatchFormKey:="SaveNotifications", MatchFormValue:="Save")> _
Function SaveNotifications(ByVal NotificationForm As FormCollection) As ActionResult
...
End Function
How do I change this so that the javascript fires, runs my validation and then hits this controller action?如何更改此设置以使 javascript 触发,运行我的验证,然后执行此 controller 操作?
Thanks谢谢
Change your submit button to将您的提交按钮更改为
<input type="submit" id="SaveNotifications" name="SaveNotifications" value="Save" class="t-button" onclick="return onSave();" />
Then in the javascript onSave() function, return true or false depending on the validation.然后在 javascript onSave() function 中,根据验证返回 true 或 false。
function onSave()
{
var isValid = true;
// ... do your validation ...
// ie:
// if (field1.value.length == 0)
// isValid = false;
// returning true will post the form. False will halt it.
return isValid;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.