2011-05-24 147 views
5

我正在一個非營利的捐款平臺,我第一次使用MVC。我已經掌握了它的大部分,但現在我有一個問題,我不知道如何解決。我使用的是AthorizeNet.Helpers類,當我從expamples添加代碼時,除了它將表單放在標籤上方之外,大部分都適用,但它將表單字段放在正確的位置。我試圖弄清楚如何在正確的地方渲染標籤。MVC HtmlHelpers麻煩與剃刀

@using AuthorizeNet.Helpers; 

@using (Html.BeginSIMForm("http://127.0.0.1:4768", 1.99M, "xxx_key", "yyy_key", true)) 
{ 
    @Html.Raw(Html.CheckoutFormInputs(true)) 
    @Html.Hidden("order_id", "1234") 
    <input type = "submit" value = "Pay" /> 
} 

這是它的外觀在HTML輸出:

<form action = 'https://test.authorize.net/gateway/transact.dll' method = 'post'> 
    <input type = 'hidden' name = 'x_fp_hash' value = '6bef386eaa89944efd62b47b86042910' \> 
    <input type = 'hidden' name = 'x_fp_sequence' value = '117'\> 
    <input type = 'hidden' name = 'x_fp_timestamp' value = 'xxx_key' \> 
    <input type = 'hidden' name = 'x_login' value = 'yyy_key' \> 
    <input type = 'hidden' name = 'x_amount' value = '1.99' \> 
    <input type = 'hidden' name = 'x_relay_url' value = 'http://127.0.0.1:4768' \> 
    <input type = 'hidden' name = 'x_relay_response' value = 'TRUE' \> 
</form><!DOCTYPE html> 
<html> 
<body> 
<h2>Payment Information</h2> 
       <div style = 'border: 1px solid #990000; padding:12px; margin-bottom:24px; background-color:#ffffcc;width:300px'>Test Mode</div> 
       <div style = 'float:left;width:250px;'> 
        <label>Credit Card Number</label> 
        <div id = 'CreditCardNumber'> 
         <input type = 'text' size = '28' name = 'x_card_num' value = '4111111111111111' id = 'x_card_num'/> 
        </div> 
       </div> 
       <div style = 'float:left;width:70px;'> 
        <label>Exp.</label> 
        <div id = 'CreditCardExpiration'> 
         <input type = 'text' size = '5' maxlength = '5' name = 'x_exp_date' value = '0116' id = 'x_exp_date'/> 
        </div> 
       </div> 
       <div style = 'float:left;width:70px;'> 
        <label>CCV</label> 
        <div id = 'CCV'> 
         <input type = 'text' size = '5' maxlength = '5' name = 'x_card_code' id = 'x_card_code' value = '123' /> 
        </div> 
       </div><input id="order_id" name="order_id" type="hidden" value="1234" />  <input type = "submit" value = "Pay" /> 

回答

5

這種問題通常發生在爲ASP.NET MVC 1編寫直接寫入輸出流的幫助程序(並且使用中包含的幫助程序大部分時間直接寫入輸出流)。在ASP.NET MVC 1,你可以使用此寫入輸出流:

htmlHelper.ViewContext.HttpContext.Response.Output 

在後來的ASP.NET MVC版本中,你應該使用這樣的:

htmlHelper.ViewContext.Writer 

這確保剃鬚刀的兼容性。如果你有權訪問AuthorizeNet.Helpers源代碼,你可以自己修復它,如果你不是,你必須聯繫作者修復它。

1

這有可能是從AuthorizeNet助手不正確寫入剃刀工作。如果沒有真正查看他們集會的來源,很難說是否是這種情況。您可能想嘗試聯繫他們的客戶支持。

12

這是我如何修復它。

正如你所說,添加@ Html.Raw()圍繞Html.CheckoutFormInputs(真)

另一個變化,使在

namespace AuthorizeNet.Helpers -> CheckoutFormBuilders.cs 

一個使用

using System.IO; 
添加

更改

HttpResponseBase to TextWriter 

我在三個位置做了這個。

HttpResponseBase _response; to TextWriter _response; 

public SIMForm(TextWriter response, string returnUrl, decimal amount, 
       string apiLogin, string transactionKey) 
       :this(response,returnUrl,amount,apiLogin,transactionKey,true){} 

public SIMForm(TextWriter response, string returnUrl, decimal amount, 
       string apiLogin, string transactionKey, bool isTest) { 
     _response = response; 
     _amount = amount; 
     _apiLogin = apiLogin; 
     _transactionkey = transactionKey; 
     _returnUrl = returnUrl; 
     _isTest = isTest; 
     OpenForm(); 
} 

兩個更多的變化留下

作指示tpeczek答案,你需要改變

helper.ViewContext.HttpContext.Response 

helper.ViewContext.Writer 

這看起來像

public static SIMForm BeginSIMForm(this HtmlHelper helper, string returnUrl, 
           decimal amount, string apiLogin, 
           string transactionKey) { 

return new SIMForm(helper.ViewContext.Writer, 
        returnUrl,amount,apiLogin, 
        transactionKey,true);} 

public static SIMForm BeginSIMForm(this HtmlHelper helper, string returnUrl, 
           decimal amount, string apiLogin, 
           string transactionKey, bool isTest) { 

return new SIMForm(helper.ViewContext.Writer, 
        returnUrl, amount, apiLogin, 
        transactionKey,isTest);} 
1

使用上面的EpicThreeDev中的示例,我能夠獲取表單html以正確顯示。但是Pay按鈕與ccv字段並置,所以爲了在Index上修復這個問題。CSHTML代碼庫,我改變了HTML來

     <br />       
    <input type = "submit" value = "Pay" style="position:relative; top:35px;" />  

一旦我做了,我是能張貼的形式,這樣做,我得到的是後處理的錯誤後,但是。

http://community.developer.authorize.net/t5/Integration-and-Testing/Having-Trouble-setting-up-MVC3-application-with-DPM/m-p/13226

0

這是我的解決方案。它只是打印Authorize.net隱藏字段,所以你可以編寫自己的表單標籤。用相關的AppSettings值填寫你的web.config。這樣,您可以輕鬆地在開發和生產之間切換。

public static class MyAuthNetHelper 
{ 
    public const string TEST_URL = "https://test.authorize.net/gateway/transact.dll"; 
    public const string LIVE_URL = "https://secure.authorize.net/gateway/transact.dll"; 

    public static string ApiLogin 
    { 
     get { return ConfigurationManager.AppSettings["AuthNetAPILogin"]; } 
    } 

    public static string TransactionKey 
    { 
     get { return ConfigurationManager.AppSettings["AuthNetAPITransactionKey"]; } 
    } 

    public static string ReturnUrl 
    { 
     get { return ConfigurationManager.AppSettings["AuthNetReturnUrl"]; } 
    } 

    public static string ThanksUrl 
    { 
     get { return ConfigurationManager.AppSettings["AuthNetThanksUrl"]; } 
    } 

    public static bool TestMode 
    { 
     get { return bool.Parse(ConfigurationManager.AppSettings["AuthNetTestMode"]); } 
    } 

    public static string GatewayUrl 
    { 
     get { return TestMode ? TEST_URL : LIVE_URL; } 
    } 

    public static MvcHtmlString AuthNetDirectPostFields(this HtmlHelper helper, decimal amount) 
    { 

     var seq = Crypto.GenerateSequence(); 
     var stamp = Crypto.GenerateTimestamp(); 

     var fingerPrint = Crypto.GenerateFingerprint(TransactionKey, 
      ApiLogin, amount, seq.ToString(), stamp.ToString()); 

     var str = new StringBuilder(); 

     str.Append(helper.Hidden("x_fp_hash", fingerPrint)); 
     str.Append(helper.Hidden("x_fp_sequence", seq)); 
     str.Append(helper.Hidden("x_fp_timestamp", stamp)); 
     str.Append(helper.Hidden("x_login", ApiLogin)); 
     str.Append(helper.Hidden("x_amount", amount)); 
     str.Append(helper.Hidden("x_relay_url", ReturnUrl)); 
     str.Append(helper.Hidden("x_relay_response", "TRUE")); 

     return MvcHtmlString.Create(str.ToString()); 
    } 
} 

CSHTML:

<form id="paymentForm" method="POST" action="@MyAuthNetHelper.GatewayUrl" class="form-horizontal offset1"> 
@Html.AuthNetDirectPostFields(PutYourAmountHere) 
... 
</form> 

ThanksUrl在你的行動來處理來自Authorize.net的響應。查看更多:http://developer.authorize.net/integration/fifteenminutes/csharp/