1

How to redirect a user from the action with POST request?

I have:

public ViewResult AddMoneyOnPay()
        {
            Onpay onpay = new Onpay();
            onpay.convert = "yes";
            onpay.pay_mode = "fix";
            onpay.price_final = "true";

            return View(onpay);
        }

View:

@using (Html.BeginForm("AddMoneyOnPay", "Transaction", FormMethod.Post))
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Onpay</legend>
        <div class="editor-field">
            @Html.EditorFor(model => model.pay_mode)
            @Html.ValidationMessageFor(model => model.pay_mode)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.price)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.price)
            @Html.ValidationMessageFor(model => model.price)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.currency)
            @Html.ValidationMessageFor(model => model.currency)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.pay_for)
            @Html.ValidationMessageFor(model => model.pay_for)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.md5)
            @Html.ValidationMessageFor(model => model.md5)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.convert)
            @Html.ValidationMessageFor(model => model.convert)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.url_success)
            @Html.ValidationMessageFor(model => model.url_success)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.url_fail)
            @Html.ValidationMessageFor(model => model.url_fail)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.user_email)
            @Html.ValidationMessageFor(model => model.user_email)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.note)
            @Html.ValidationMessageFor(model => model.note)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.one_way)
            @Html.ValidationMessageFor(model => model.one_way)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.price_final)
            @Html.ValidationMessageFor(model => model.price_final)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.direct_no)
            @Html.ValidationMessageFor(model => model.direct_no)
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

action POST

[HttpPost]
        public ViewResult AddMoneyOnPay(Onpay onpay)
        {
            string secretKEy = "fH12312IO";
            onpay.md5 = GetHash(string.Format("{0};{1};{2};{3};{4}", onpay.pay_mode, onpay.price, onpay.currency, onpay.pay_for, onpay.convert, secretKEy));
            //SEND POST to url http://paysite.ru/blablabla
        }
Mediator
  • 14,951
  • 35
  • 113
  • 191

1 Answers1

1

Have your AddMoneyOnPay method return an ActionResult and then write:

return Redirect(returnUrl);
Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
  • 2
    The post data will be populated into your `Onpay` object - do all of your processing of it in your POST overload of `AddMoneyOnPay()`, then redirect the user using `Redirect()`. – Steve Wilkes Feb 22 '12 at 15:40
  • @simply denis - in other words - do it in two steps. Process your data from a POST in `AddMoneyOnPay(Onpay onpay)` and then, once processed and saved (or whatever), redirect the user. Redirecting is always a `GET`. There is one other theoretical option - a 307 "temporary redirect" but in practice it isn't really a solution I don't think. Have a look at this answer: http://stackoverflow.com/questions/46582/response-redirect-with-post-instead-of-get – Rob Levine Feb 23 '12 at 10:19
  • @simplydenis, in your action replace the `return View(onpay);` line with `return Redirect(returnUrl);` where `returnUrl` will be pointing to the url you are willing to redirect. – Darin Dimitrov Feb 23 '12 at 10:19