MVC HtmlHelpers 与 Razor 的问题 [英] MVC HtmlHelpers trouble with Razor

查看:24
本文介绍了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")<输入类型=提交"值=支付"/>}

这是它在 HTML 输出中的样子:

<输入类型 = '隐藏' 名称 = 'x_fp_hash' 值 = '6bef386eaa89944efd62b47b86042910' ><输入类型 = '隐藏' 名称 = 'x_fp_sequence' 值 = '117'><input type = 'hidden' name = 'x_fp_timestamp' value = 'xxx_key' ><input type = 'hidden' name = 'x_login' value = 'yyy_key' ><输入类型 = '隐藏' 名称 = 'x_amount' 值 = '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><身体><h2>支付信息</h2><div style = 'border: 1px solid #990000;填充:12px;底边距:24px;background-color:#ffffcc;width:300px'>测试模式</div><div style = 'float:left;width:250px;'><label>信用卡号</label><div id = 'CreditCardNumber'><输入类型 = '文本' 大小 = '28' 名称 = 'x_card_num' 值 = '4111111111111111' id = 'x_card_num'/>

<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 style = 'float:left;width:70px;'><label>CCV</label>

<输入类型 = '文本' 大小 = '5' maxlength = '5' 名称 = 'x_card_code' id = 'x_card_code' value = '123'/>

</div><input id="order_id" name="order_id" type="hidden" value="1234"/><输入类型=提交"值=支付"/>

解决方案

这种问题通常发生在为 ASP.NET MVC 1 编写直接写入输出流的帮助程序时(并且包含在 using 中的帮助程序是大多数情况下直接写入输出流).在 ASP.NET MVC 1 中,您可以使用以下命令写入输出流:

htmlHelper.ViewContext.HttpContext.Response.Output

在以后的 ASP.NET MVC 版本中你应该使用这个:

htmlHelper.ViewContext.Writer

这确保了 Razor 的兼容性.如果您可以访问 AuthorizeNet.Helpers 源代码,您可以自行修复它,否则您必须联系作者进行修复.

I'm working on a non-profit donation platform and I'm using MVC for the first time. I've got the hang of it for the most part but right now I'm having a problem that I dont know how to address. I'm using the AthorizeNet.Helpers class and when I add the code from the expamples, it works for the most part except for it takes the form and puts it ABOVE the tag however it puts the form fields in the correct place. I'm trying to figure out how to render the tag in the correct place.

@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" />
}

This is how it looks in HTML output:

<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" />

解决方案

This kind of problem usually happens when a helper which is writing directly to output stream was written for ASP.NET MVC 1 (and helpers that are enclosed in using are writing directly to output stream most of the time). In ASP.NET MVC 1 you could write to output stream by using this:

htmlHelper.ViewContext.HttpContext.Response.Output

In later ASP.NET MVC version you should be using this:

htmlHelper.ViewContext.Writer

That ensures Razor compatibility. If you have access to AuthorizeNet.Helpers source code you can fix it by yourself, if you don't than you have to contact authors for fixing it.

这篇关于MVC HtmlHelpers 与 Razor 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆