3

我正試圖在剃鬚刀中使用mvc4進行支付網關集成。在這一點上,我需要用預填寫的表單來調用頁面。使用MVC 4中的POST參數重定向支付網關集成

使用下面的方法,我形成了POST方法形式:

private static string PreparePOSTForm(string url, System.Collections.Hashtable data)  // post form 
    { 
     //Set a name for the form 
     string formID = "PostForm"; 
     //Build the form using the specified data to be posted. 
     StringBuilder strForm = new StringBuilder(); 
     strForm.Append("<form id=\"" + formID + "\" name=\"" + 
         formID + "\" action=\"" + url + 
         "\" method=\"POST\">"); 

     foreach (System.Collections.DictionaryEntry key in data) 
     { 

      strForm.Append("<input type=\"hidden\" name=\"" + key.Key + 
          "\" value=\"" + key.Value + "\">"); 
     } 

     strForm.Append("</form>"); 
     //Build the JavaScript which will do the Posting operation. 
     StringBuilder strScript = new StringBuilder(); 
     strScript.Append("<script language='javascript'>"); 
     strScript.Append("var v" + formID + " = document." + 
         formID + ";"); 
     strScript.Append("v" + formID + ".submit();"); 
     strScript.Append("</script>"); 
     //Return the form and the script concatenated. 
     //(The order is important, Form then JavaScript) 
     return strForm.ToString() + strScript.ToString(); 
    } 

而在我的控制頁面我與所需的參數調用PreparePostForm和我收到的POST請求的格式。

[HttpPost] 
public ActionResult OrderSummary() 
     { 
      string request=PreparePOSTForm("payment URL","hashdata required for payment") 
      return Redirect(request); 
     } 

但是,雖然重定向我越來越低於錯誤。

錯誤的請求 - 無效的URL

HTTP 400錯誤的請求URL無效。

我錯過了這裏使用POST請求的東西。有人能幫我嗎。

在此先感謝。

回答

2

您無法通過Redirect方法發佈表單。您可以將生成的表單字符串發送到View,然後在Javascript之後發送表單。

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

並在OrderSummary查看:

@model string 

@Html.Raw(Model) 

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

很簡單明瞭。謝謝! –

0

我建議你創建一個Action與表格,它在參數中接收Model。然後,只要通過模型,當你重定向到這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 
    } 
} 
0

你可以用JavaScript來做到這一點。

製作一個頁面,它有一個html表單並通過javascript提交,並將您的信息作爲輸入:隱藏在表單中。

這將提交數據到你想要的另一個地方。在html中做這件事給了你更多的控制權,並且你不需要爲你的應用程序中的每個重定向都寫出其他答案中所示的動作。