2015-09-16 34 views
3

我是MVC的新手。
其實我在ASP.Net這樣一個場景,我submitting data(其中包含金額,等的redirectUrl)向URL(支付網關)是這樣的:如何將數據發佈到MVC的操作URL

<form id="form1" runat="server" method="post" action="https://test2pay.ghl.com/IPGSG/Payment.aspx"> 
     <div> 
      <%=eGHLPurchaseItems%> 
      <input type="submit" value="Pay Now" /> 
     </div> 
    </form> 

,它會redirect to that payment gateway page後成功交易,我會redirected back to my application頁與一些extra status codes

我被HttpContext.Current.Request["TransactionType"];

處理這些返回的狀態值現在,我需要做的是,在MVC,但我唯一的困惑是如何提交表單。

我在MVC使用的嘗試:

@using (@Html.BeginForm("https://test2pay.ghl.com/IPGSG/Payment.aspx", null, FormMethod.Post)){ 
     <button type="submit" class="btn btn-lg b-primary">Proceed to Payment</button> 
     } 

但是,我重新導向到這個網址:

http://localhost:62414/Billing/https%3a/test2pay.ghl.com/IPGSG/Payment.aspx 

誰能請幫助我如何我可以使用提交表單MVC中的一些數據?

+0

如果數據是POST,它不在URL中。它是將所有數據作爲參數讀取的GET方法。您始終可以添加隱藏字段或使用GET。注意:請勿將敏感信息放入發送給客戶的任何內容(HTML或URL)中。如果數據很敏感,請在您的應用程序中使用一些頁面作爲代理。 – SJuan76

+2

不要使用'Html.BeginForm()'手動創建表單標籤 –

+0

實際上,我手動保留了表單標籤,但是我無法將數據傳遞給表單。 我試過@ Html.Hidden(「ABC」,@ViewData [「ABCDATA」])。 但它顯示了一些web.config錯誤,並帶出隱藏值,表單正在提交 – RealSteel

回答

2

要麼創建HTML的標籤或使用

您的控制器像

ViewBag.CustId = "101"; 
ViewBag.Email = "[email protected]"; 

,並查看

@Html.BeginForm(null, null, FormMethod.Post, new {@action="https://test2pay.ghl.com/IPGSG/Payment.aspx"}) 
     { 
     @Html.Hidden("id", @ViewBag.CustId); 
     @Html.Hidden("email", @ViewBag.Email); 
      <button type="submit" class="btn btn-lg b-primary">Proceed to Payment</button> 
     } 

既然你正試圖重定向到外部URL,設置ActionController到空值。

+0

標記你的一段代碼也工作fine.Do你知道如何將數據傳遞給它? 我在ViewData中獲取數據 – RealSteel

+0

我想你的支付網關提供商必須爲你提供數據格式。你可以像@eGHLPurchaseItems – Imad

+0

那樣傳遞它......實際上這是用戶定義的變量。它可以是任何名稱。 實際上,我通過了一些Input隱藏標籤,例如:string PymtMethodTemplate =「」;等等。我終於準備了一些輸入隱藏字段到一個字符串,然後傳遞數據。 – RealSteel

0

你也可以做一個ajax文章,它比簡單地發佈表單到遠程URL有一些優點。最重要的是,它使您有機會優雅地處理該帖子可能發生的任何錯誤。

<form action="https://test2pay.ghl.com/IPGSG/Payment.aspx" name="frmPost"> 

</form> 

$("form").submit(function() { 
    $.post($(this).attr("action"), $(this).serialize(), function(data) { 
     // here's where you check the response you got back from the post 
     if (!data.IsOK) 
     { 
      // the post didn't succeed... handle error here or something 
     } 
     else 
     { 
      // the post succeeded so redirect back to this page 
      window.location.href = '@Url.Content("~/")`; 
     } 
    }); 
}); 

但是,在實施此操作之前,您需要考慮幾件事情。

  1. 您發佈的URL是否接受序列化的JSON數據?
  2. 它會返回什麼數據,以便讓您知道您的帖子發生了什麼?

我不能幫你第一次考慮,但對於第2次,我強烈建議使用Fiddler Web Debugger來檢查帖子的結果。

HTH

0

在你的模型:

public string eGHLPurchaseItems { get; set; } 

什麼就做什麼,你做的設置eGHLPurchaseItems ...

在你看來

@using(Html.BeginForm(null, null, FormMethod.Post, new { id = "myForm" , @action="https://test2pay.ghl.com/IPGSG/Payment.aspx"})) 
      { 
      @Html.HiddenFor(model => model.eGHLPurchaseItems) 
       <button type="submit" class="btn btn-lg b-primary">Proceed to Payment</button> 
      } 

<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#myForm").submit(); 
    }); 
</script> 
相關問題