Submit Ajax.BeginForm via ActionLink

2020-07-26 11:38发布

问题:

I am trying to submit an Ajax.BeginForm using an hyperlink as opposed to a Submit Button. I tested with the submit button and the action recognizes the post as Ajax based by test Request.IsAjaxRequest, however if I try the following IsAjaxRequest returns false:

<a href="javascript:void(0)" onclick="javascript:document.forms[0].submit(); return false;">Update</a>

回答1:

The form is hooked into Microsoft's Ajax library, so you can't just call form.submit() because the wired up ajax events aren't activated. You have a few options:

  • Use a submit button instead of a link
  • Drop the MS Ajax stuff and manually wire up your ajax posts with jQuery (this would be what I would have done)
  • Call the MS Ajax submit function

For the third option, instead of

onclick="javascript:document.forms[0].submit(); return false;"

Try

onclick="javascript:$('#form').onSubmit(); return false;"

But I've never used it so I don't know if it will work. You might also take a look here for the solution, as it sounds exactly like what you are trying to accomplish.