简体   繁体   中英

Event when form submittion ends

I have this:

<form id="import_form" method="post" enctype="multipart/form-data" action="/Blah/Blah">
    <input type="file" id="fileUpload" name="fileUpload"/>
    <input type="submit" name="submit" value="Import"/>
</form>

$('#import_form').submit(function () {
    ...
});

Here is the c# method:

[AcceptVerbs(HttpVerbs.Post)]
public string Blah(HttpPostedFileBase fileUpload, FormCollection form)
{ ... }

I want when Blah finishes executing a javacript code to start executing. How to do this?
The submit event is called before it.

This depends on how the form is being submitted.

If you're submitting the form via AJAX then you can execute some JavaScript in the handler for the response. However, given that there's a submit button, I'm assuming for the moment that you're not doing this via AJAX and are instead posting the whole page to the server and rendering a response.

In this case, to execute some JavaScript after the form post, you're going to need to render that JavaScript in the response from the server as part of the next page. When the server constructs the view, include the JavaScript you want to execute in that view.

Keep in mind the request/response nature of the web. When something on the server executes, the client is unaware of it and disconnected from it. The end result of any server-side processing should be an HTTP response to the client. In the event of submitting a form or clicking a link or anything which results in a page reload, that response is in the form of a new page (view). So anything that you want to do on the client after the server-side processing needs to happen as part of that response.

Edit: I just noticed that Blah is returning a string . Is this even working for you? How does the form submit result in a new view? Or am I unaware of a feature in ASP.NET MVC?

This is what I did in the end.

My form returns the exact same view with whom it was called.

I add a ViewData in the Blah method.

In the view, in the $(function()) event I check if ViewData has a value, I execute the javascript code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM