Redirect with POST parameters in MVC 4 for Payment Gateway Integration

十年热恋 提交于 2019-12-01 21:39:50
Mohsen Esmailpour

You cannot post a form by Redirect method. You could send generated form string to View and after that post the form by Javascript.

public ActionResult OrderSummary()
{
    string request=PreparePOSTForm("payment URL","hashdata required for payment")
    return View(model:request);
}

and in View of OrderSummary:

@model string

@Html.Raw(Model)

<script>
    $(function(){
       $('form').submit();
    })
</script>

You can do it with JavaScript.

Make a page which does have a html form and submit it through javascript and put your information as input:hidden in the form.

This will submit the data to the another place you want. Doing it in html gave you more control and you doesn't need to write a action like shown in other answer for every redirect in your app.

I suggest you to create an Action with form which receives a Model in parameters. Then, just pass model when you redirect to this Action

[HttpPost]
public ActionResult OrderSummary()
{
    return RedirectToAction("OrderForm", new { HashData = hashData });
}

[HttpGet]
public ViewResult OrderForm(string hashData)
{
    OrderFormModel model = new OrderFormModel();
    model.HashData = hashData;
    return View(model);
}

[HttpPost]
public ActionResult OrderForm(OrderFormModel model)
{
    if(ModelState.IsValid)
    {
        // do processing
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!