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¤cy=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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…