I'm new to MVC, and this problem has been driving me up the wall. I have some javascript that triggers a jquery ajax post when the user press the tab or enter key in the textboxes on my form:
<script type="text/javascript"> $(document).ready(function () { $('#RmaNumber, #SerialNumber').keydown(function (event) { if (event.keyCode == 13 || event.keyCode == 9) { var ri = { RmaNumber: $('#RmaNumber').val(), SerialNumber: $('#SerialNumber').val(), ControlName: event.target.id } $.ajax({ type: "POST", url: "/Invoice/BarcodeScan", contentType: "application/json; charset=utf-8", data: JSON.stringify(ri), dataType: "json", success: function (data) { $('#TerminalType').text(data.TerminalType); } }); } }); }); </script> Here is what my controller looks like. I removed the code to keep things simple:
public ActionResult Index() { } [AcceptVerbs(HttpVerbs.Post)] public ActionResult Index(RepairInvoice ri) { } [HttpPost] public ActionResult BarcodeScan(string RmaNumber, string SerialNumber, string ControlName) { } The ajax postback causes both the BarcodeScan and Index action to fire. I only want the Index action with the [AcceptVerbs(HttpVerbs.Post)] above it to fire if a button is pressed on my form. Is this possible, or am I on the wrong track?
BarcodeScanand the form itself is posting toIndex. Also, if you don't want theBarcodeScanaction to be invoked, why are you invoking it in the JavaScript code?inputis asubmitthen making it abuttonmay prevent the default post. UsingpreventDefault()in the JavaScript may work as well.