BVernon
BVernon

Reputation: 3787

How to post without redirecting?

I want to use something like this:

    @using(Html.BeginForm("UploadFile", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    { 
        <label for="file">Filename:</label>
        <input type="file" name="file" id="file" />
        <input type="submit" value="Upload File" />
    }

but this is one of multiple steps on a single page application. I don't want it to redirect the user another view associated with the UploadFile action... I just want it to submit the data and stay put. How can I prevent it from redirecting?

Upvotes: 2

Views: 2262

Answers (1)

Eric Matson
Eric Matson

Reputation: 84

You can use Ajax.BeginForm instead of Html. The ajax helper has a lot of the same features but it does everything through an ajax call. You will need to include the unobtrusive java script files for this

https://msdn.microsoft.com/en-us/library/system.web.mvc.ajax.ajaxextensions.beginform(v=vs.118).aspx

Edit Here is a sample

Here is the code sample -

@using (Ajax.BeginForm( actionName: "Edit",
                            controllerName: "Home",
                            routeValues: null,
                            ajaxOptions: new AjaxOptions() { HttpMethod = "POST", OnSuccess = "RecordUpdated", OnFailure = "AjaxFailed" },
                            htmlAttributes: new { id = "editForm" }))
{
    <!-- Form Body -->
}

you also have to include the jquery.unobtrusive-ajax.js at the bottom of your page, so the form will get wired up by the javascript

Upvotes: 2

Related Questions