Click here to Skip to main content
15,886,678 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here is my code

/********Controller**********/

C#
public JsonResult GetTransactionBuyingRate(int? eventIdB, decimal? NRB)
        {
            var currFrom = db.Currencys.Where(u => u.CurrencyId == eventIdB).First();
            Session["NewBTrate"] = currFrom.Buying_Rate;
            NRB = currFrom.Buying_Rate;
            var result = currFrom.Buying_Rate;
            return Json(result, JsonRequestBehavior.AllowGet);
        }



/**********Javascript Ajax Function**************/

C#
<script type="text/javascript">
        function GetTransactionBuyingRate()
                    {                  
            var eventIdB = document.getElementById("CurrencyId").value;
            var NRB;
            alert(eventIdB);
                        $.ajax(
                            {
                                url: '@Url.Action("GetTransactionBuyingRate", "Transaction")',
                                type: 'GET',
                                data: { eventIdB: eventIdB },
                                cache: false,
                                success: function (result) {
                                    var NCRB = '@Session["NewBTrate"]';   
                                    alert(NCRB);
                                    $('#Transaction_Rate').val(NCRB);
                                }
                            });
                        }
        </script>

Can someone help me return value to the decimal variable 'NRB' and assign it to '$('#Transaction_Rate').val()' after ajax function completes???
Posted
Updated 12-Apr-14 12:26pm
v2
Comments
Abinash_Sahoo 12-Apr-14 16:36pm    
did you try without quote? : var NCRB = @Session["NewBTrate"];
Member 10471426 12-Apr-14 16:51pm    
Yes I did and had syntax error..
Pheonyx 12-Apr-14 18:48pm    
Have you tried alert(result) ?
The value of the result object, should the result you are returning from your action.
Member 10471426 12-Apr-14 20:35pm    
Yes I have tried that too but the result is undefined...
Member 10471426 13-Apr-14 2:05am    
Hi, its works now, thanks people......

1 solution

I think you are doing it wrong. You have a controller action with decimal? NRB as a parameter which you are not passing value to from your ajax call but you setting that variable and then trying to some how use it.

I think you could have your controller as shown below:

public JsonResult GetTransactionBuyingRate(int? eventIdB, decimal? NRB)
{
    var currFrom = db.Currencys.Where(u = > u.CurrencyId == eventIdB).First();
    return Json(currFrom.Buying_Rate, JsonRequestBehavior.AllowGet);
}


<script type="text/javascript">
    function GetTransactionBuyingRate() {
        var eventIdB = document.getElementById("CurrencyId").value;    
        $.ajax({
            url: '@Url.Action("GetTransactionBuyingRate", "Transaction")',
            type: 'GET',
            data: {
                eventIdB: eventIdB
            },
            cache: false,
            success: function(result) {
                alert(result);
                $('#Transaction_Rate').val(result);
            }
        });
    }
</script>


Also would suggest you to open your Dev tools (f12) and check for errors if any and network calls.
 
Share this answer
 
v2

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