MVC HtmlHelpers遇到Razor问题

| 我正在使用一个非营利性捐赠平台,并且是第一次使用MVC。我大部分都掌握了它,但是现在我遇到了一个我不知道该如何解决的问题。我使用的是AthorizeNet.Helpers类,当我从示例中添加代码时,它在大多数情况下都可以工作,除了它采用表格并将其放在标签上方,但是将表格字段放在正确的位置。我正在尝试弄清楚如何在正确的位置呈现标记。
@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\" />
    
已邀请:
当直接为输出流编写的辅助程序是为ASP.NET MVC 1编写的时,通常会发生此类问题(并且使用附带的帮助程序大多数时候都直接写入输出流)。在ASP.NET MVC 1中,可以使用以下命令写入输出流:
htmlHelper.ViewContext.HttpContext.Response.Output
在更高版本的ASP.NET MVC中,您应该使用以下版本:
htmlHelper.ViewContext.Writer
这样可以确保Razor的兼容性。如果您有权访问AuthorizeNet.Helpers源代码,则可以自己进行修复,如果您不这样做,则必须联系作者进行修复。     
这就是我解决的方法。 如您所述,在Html.CheckoutFormInputs(true)周围添加@ Html.Raw() 要做的另一项改变是
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);}
    
来自AuthorizeNet的帮助程序可能编写不正确,无法与Razor一起使用。如果不查看组装的源代码,很难说是这样。您可能想尝试与他们的客户支持联系。     
使用上面的EpicThreeDev中的示例,我能够使html表单正确显示。但是,“付款”按钮并置在ccv字段上方,因此为了解决此问题,我将HTML更改为Index.cshtml代码库
                     <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     
这是我的解决方案。它只是打印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>
在您的操作中使用thankUrl处理来自Authorize.net的响应。进一步检查:http://developer.authorize.net/integration/fifteenminutes/csharp/     

要回复问题请先登录注册