Click here to Skip to main content
11,567,539 members (42,738 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)114.6K
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


Advertise | Privacy | Mobile
Web03 | 2.8.150624.2 | 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