MVC3中具有共享cshtml视图的单独javascript文件 [英] Separate javascript files with shared cshtml views in MVC3

查看:77
本文介绍了MVC3中具有共享cshtml视图的单独javascript文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

如何使用通用的get,post方法,并使用面向对象的JavaScript和JQuery将参数从cshtml传递到计算js文件.

这是我的common.js文件和Calculation.js文件

common.js

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


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);
}


调试后转到"OnErrorCall"函数

谢谢问候,
Soumya


After debugging it goes to ''OnErrorCall'' function

Thanks& Regards,
Soumya

推荐答案

.extend({},Mvcdemo_post_send.common.DefaultOptions,options);
.extend({}, Mvcdemo_post_send.common.DefaultOptions, options);


.ajax({ 缓存:ajaxOptions.cache, 类型:' POST', 网址:requestUrl, 数据:dataStringified, contentType:ajaxOptions.contentType, dataType:ajaxOptions.dataType, 成功:onSuccess, 错误:onError }); }, GetData:函数(requestUrl,dataStringified,选项,onSuccess,onError){ var ajaxOptions =
.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);
.extend({}, Mvcdemo_post_send.common.DefaultOptions, options);


这篇关于MVC3中具有共享cshtml视图的单独javascript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆