I use an Ajax form i MVC开发者_运维知识库 3. I want to trigger the submit with:
$("form").submit();
and I want to do a callback function after the form submits successfully, how can I do that?
There's no good way to do it with just client-side/jQuery and retain that state after the submission, because the page gets reloaded.
You're better off setting some kind of a server-side variable once you're in your Action after the form submit. And then reference that variable in the View when you go to render it again. Or better yet, have that variable be a property on your Model, which gets set after submit. Perhaps something like this:
public ActionResult(MyModel model)
{
// Do post-submit stuffs with model.
model.Submitted = true;
return View(model);
}
And then in the view, you can do something like:
<% if (Model.Submitted) { %>
<script type="text/javascript">
doPostSubmitStuffs();
</script>
<% } %>
I'm using the validate plugin and I include the submitHandler
option:
$("#theForm").validate({
submitHandler: function(form) {
$.post('patient/save',//url
{ //data },
function (data) {
//callback function
}
);
}
});
` Here is the documentation: http://docs.jquery.com/Plugins/Validation
精彩评论