简体   繁体   中英

Client Side Redirect on Form Submission

I have a cshtml page that makes a call to a controller method and passes data to that method via a jquery call on form submission as follows:

$('form').submit(function () {

    //create instance for datePicker.
    // only after control creation we can get dateObj otherwise it throws exception.
    var dateObj = $("#datepick").ejDatePicker('instance');
    var obj = dateObj.option('value')

        //$.datepicker.formatDate('dd/MM/yyyy', new Date());
    var d = new Date(2011, 10, 30);
    alert(obj);
    alert(d);

    $.ajax({
        url: '/AutoComplete/AutocompleteFeaturesNew',
        data: {
            RequestedUntilDate: obj.toISOString(),
            RoomNo: '13a'
        },
        type: 'POST',
        success: function (data) {
            alert("key is suucessfully got in controller through form submit, Key:" + data);
        }
    });
});

The controller method itself is as follows:

[HttpPost]
public JsonResult AutocompleteFeaturesNew (RequestCreateViewModel createRequestViewModel)
{
    return Json(createRequestViewModel.RequestedUntilDate);
}

What I want to be able to do is, once the form has been submitted is to re-direct the control either to a) another page b) another controller method on successful submission of the form.

So, something like:

return RedirectToAction("Index");

But on the client side.

Any suggestions?

通过将以下行添加到ajax成功函数主体中,您只能通过javascript做到这一点:

window.location.replace("http://...");

Thanks for all your replies.

What I needed to do was to ensure that the $('form').submit(function () { returns false.

So:

//bind below onClick action to button $('form').submit(function () {

        //create instance for datePicker.
        // only after control creation we can get dateObj otherwise it throws exception.
        var dateObj = $("#datepick").ejDatePicker('instance');
        var obj = dateObj.option('value')

        //$.datepicker.formatDate('dd/MM/yyyy', new Date());
        var d = new Date(2011, 10, 30);
        alert(obj);
        alert(d);

        $.ajax({
            url: '/AutoComplete/AutocompleteFeaturesNew',
            data: {
                RequestedUntilDate: obj.toISOString(),
                RoomNo: '13a'
            },
            type: 'POST',
            dataType: 'json',
            success: function () {
                window.location.replace("http://stackoverflow.com/questions/41719249/client-side-redirect-on-form-submission?noredirect=1#comment70633597_41719249");
                }
        });
        return false;
    });

Thanks again,

Sean

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