Click here to Skip to main content
15,120,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to get the viewbag value, added a condition and based on that the alert message popsup. Below is the code i have written in jquery.

JavaScript
$("#btnReviewAndSave").click(function () {
    var curSum = '@ViewBag.curSum';
    var cdPayment = '@ViewBag.CdPayment';
    var cdPaymentTotal = cdPayment + 200;
    if (curSum > cdPayment + 200) {
        if (!confirm('The Advertising Payment of $' + curSum + ' exceeds the allocated Advertising amount by $ ' + cdPaymentTotal + '. Do you want to Save?')) {
            return false;
        }
    }
});


But when I click on the button, alert comes without value. when i debug, I could see the viewbag value as "Undefined". What could be the issue? and How to declare it?
Anyone please help me out.

The alert message is "The Advertising Payment of $@ViewBag.curSum
exceeds the allocated Advertising amount by $@ViewBag.CdPayment200. Do you want to Save?".

What I have tried:

Trying to get the Viewbag value from controller to view. But it is as Undefined when used in jquery
Posted
Updated 22-Mar-16 2:15am
v2
Comments
Kornfeld Eliyahu Peter 1-Mar-16 4:57am
   
Your code may be all right, but it depends on the type of the value you try to access...
So what type are they?
kruthi Mallarapu 1-Mar-16 5:17am
   
Its decimal value.
F-ES Sitecore 22-Mar-16 7:17am
   
Is your javascript in an external .js file?

1 solution

Razor
Try this
var curSum = parseFloat(@ViewBag.Number);
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900