Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

c# - Problem passing Razor Value to js <script> inside a view mvc asp.net ef

I' ll start this by saying that yesterday the code was working fine. I can't figure out what I'm doing wrong, I think this should be working but it doesn't so thank you in advance for all the help:

I have this view and I' m trying to implement Paypal Paylemnet. This is the script section in the view:

    <div id="smart-button-container">
    <div style="text-align: center;">
        <div id="paypal-button-container"></div>
    </div>
</div>



@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")



    <script src="https://www.paypal.com/sdk/js?client-id=sb&currency=EUR" data-sdk-integration-source="button-factory"></script>
    <script>
    function initPayPalButton() {
        paypal.Buttons({
            style: {
                shape: 'rect',
                color: 'gold',
                layout: 'vertical',
                label: 'paypal',

            },

            createOrder: function (data, actions) {
                var totalPrice = @Model.OrderDetailsVMs.Sum(x => x.TotalPricePerOrderDetails);
                return actions.order.create({
                    purchase_units: [{ "amount": { "currency_code": "EUR", "value": (totalPrice + 10), "breakdown": { "item_total": { "currency_code": "EUR", "value": totalPrice }, "shipping": { "currency_code": "EUR", "value": 10 }, "tax_total": { "currency_code": "EUR", "value": 0 } } } }]
                });
            },

            onApprove: function (data, actions) {
                return actions.order.capture().then(function (details) {
                    alert('Transaction completed by ' + details.payer.name.given_name + '!');
                });
            },

            onError: function (err) {
                console.log(err);
            }
        }).render('#paypal-button-container');
    }
    initPayPalButton();
    </script>
}

The problem comes in this line :

var totalPrice = @Model.OrderDetailsVMs.Sum(x => x.TotalPricePerOrderDetails);

The value doesn't pass from the razor to the js var. If I hardcode a value to var totalPrice, for example if I write:

var totalPrice = 3.33;

everything works fine. Also, I can display the value of the

@Model.OrderDetailsVMs.Sum(x => x.TotalPricePerOrderDetails); 

and it works, so I know that the value passes there. Can anyone think of why the value doesn't pass inside the script?

P.S I get the same problem even if I try to pass it through an id.

Thank you all in advance.

Edit : Fixed it was using a decimal value with more than two digits after the .

question from:https://stackoverflow.com/questions/65863745/problem-passing-razor-value-to-js-script-inside-a-view-mvc-asp-net-ef

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...