Click here to Skip to main content
11,648,094 members (75,434 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MVC3
Hi All,

How can I use a common get,post method and pass the parameter from cshtml to calculation js file using object oriented JavaScript and JQuery.

Here is my common.js file and calculation.js file

common.js
if (typeof Mvcdemo_post_send === 'undefined') Mvcdemo_post_send = {};
Mvcdemo_post_send.common = {
    PostData: function (requestUrl, dataStringified, options, onSuccess, onError) {
        var ajaxOptions = $.extend({}, Mvcdemo_post_send.common.DefaultOptions, options);
        $.ajax({
            cache: ajaxOptions.cache,
            type: 'POST',
            url: requestUrl,
            data: dataStringified,
            contentType: ajaxOptions.contentType,
            dataType: ajaxOptions.dataType,
            success: onSuccess,
            error: onError
        });
    },
    GetData: function (requestUrl, dataStringified, options, onSuccess, onError) {
        var ajaxOptions = $.extend({}, Mvcdemo_post_send.common.DefaultOptions, options);
        $.ajax({
            cache: ajaxOptions.cache,
            type: 'GET',
            url: requestUrl,
            data: dataStringified,
            contentType: ajaxOptions.contentType,
            dataType: ajaxOptions.dataType,
            success: onSuccess,
            error: onError
        });
    }
}       
    }
Mvcdemo_post_send.common.DefaultOptions = {
    cache: false,
    contentType: 'application/json; charset=utf-8',
    dataType: 'json'
}
calculation.js
if (typeof Mvcdemo_post_send === 'undefined') Mvcdemo_post_send = {};
//Mvcdemo_post_send.calculation = (function () {
$('#Submit1').live('click', function (e) {
    var fisrtNumber = $('#txtfirstno').val();
    var secondNumber = $('#txtsecondno').val();
    var params = '{fisrtNumber=:' + fisrtNumber + '&secondNumber=' + secondNumber+'}';
//        Mvcdemo_post_send.common.showalert();
    Mvcdemo_post_send.common.GetData("Home/AddNumbers", params, null, OnSuccessCall, OnErrorCall);
});
//});
var OnSuccessCall = function (result) {
 
        $('#txtAddNumbers').val(result);
}
var OnErrorCall = function (request, status, error) {
 
    alert(request.status + " " + request.statusText);
}
After debugging it goes to 'OnErrorCall' function

Thanks& Regards,
Soumya
Posted 16-Oct-12 23:17pm
soumyaraj1.2K
Edited 18-Oct-12 3:06am
(no name)115.2K
v3
Comments
soumyaraj at 19-Oct-12 6:22am
   
I could solve myself.here is the code in calculation.js

if (typeof Mvcdemo_post_send === 'undefined') Mvcdemo_post_send = {};
//Mvcdemo_post_send.calculation = (function () {
$('#Submit1').live('click', function (e) {

var fisrtNumber = $('#txtfirstNumber').val();
var secondNumber = $('#txtsecondNumber').val();
var params = 'fisrtNumber=' + fisrtNumber + '&secondNumber=' + secondNumber ;


Mvcdemo_post_send.common.GetData('/Home/AddNumbers', params, null, OnSuccessCall_calc, OnErrorCall_calc);
});
//});
var OnSuccessCall_calc = function (result) {

$('#txtAddNumbers').val(result);
}
var OnErrorCall_calc = function () {

alert(request.status + " " + request.statusText);

}

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

  Print Answers RSS
0 F-ES Sitecore 395
1 DamithSL 305
2 OriginalGriff 297
3 CPallini 250
4 Kornfeld Eliyahu Peter 180
0 OriginalGriff 1,342
1 jyo.net 994
2 DamithSL 981
3 Sergey Alexandrovich Kryukov 893
4 CPallini 845


Advertise | Privacy | Mobile
Web04 | 2.8.150804.4 | Last Updated 18 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100