调用控制器Actinresult从javascript带参数的类型动作

时间:2012-10-08 06:38:07

标签: javascript ajax asp.net-mvc-3

我尝试按照以下方式:

我的行动是:

[HttpPost]
        public ActionResult PaymentVoucherCommit(string sParameter)
        {
            try
            {
                _oVoucher = new Voucher();
                _oVoucher = _oVoucher.CommitVoucherNo(2, 1); // Here 2 refere VoucherTypeID that is PaymentVoucher & 1 refere jam company ID
                _oVoucher.BaseCurrencyId = 1; //jas......this code is temporary 
                _oVoucher.CompanyID = 1;//jas......this code is temporary 
                _oVoucher.VoucherTypeID = 2;//jam for temporary basis code 2 is paymenttypeid that is payment voucher
                _oVoucher.CurrencyId = 1;
                _oVoucher.BaseCurrencyNameSymbol = "Taka[Tk]"; //jas......this code is temporary
                _oVoucher.VoucherDetailLst = VoucherDetail.Gets(_oVoucher.VoucherID);
                _oVoucher.LstCurrency = Currency.Gets();
                _oVoucher.Operation = "AddPaymnetVoucher";
                _oVoucher.DebitAccountHeadName = "Press Enter";
                _oVoucher.CreditAccountHeadName = "Press Enter";
                return View(_oVoucher);
            }
            catch (Exception ex)
            {
                return View(ex.Message);
            }
        }

我的javascript代码是:

$('#btnCommit').keypress(function (e) {
        debugger;
        var keyCode = e.keyCode || e.which;
        if (keyCode == 13) {
            $.ajax({
                type: "POST",
                dataType: "text json",
                url: '@Url.Action("PaymentVoucherCommit", "Voucher")',
                data: { sParameter: "Bangladesh" },
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    //                        debugger;
                    alert(data);
                },
                error: function (xhr, status, error) {
                    alert(error);
                }
            });
            return false;
        }
    })

请修复此错误。

注意:我为[httpGet]请求成功完成了相同类型的任务。但我是一个三元组的邮政类型行动(行动结果)

1 个答案:

答案 0 :(得分:0)

尝试简单:

$('#btnCommit').keypress(function (e) {
    debugger;
    var keyCode = e.keyCode || e.which;
    if (keyCode == 13) {
        $.post('@Html.Raw(Url.Action("PaymentVoucherCommit", "Voucher"))', { sParameter: "Bangladesh" }, function(view){
             alert(view);
        });
        return false;
    }
})

如果你想返回Json,那么:

[HttpPost]
    public JsonResult PaymentVoucherCommit(string sParameter)
    {
        try
        {
            _oVoucher = new Voucher();
            _oVoucher = _oVoucher.CommitVoucherNo(2, 1); // Here 2 refere VoucherTypeID that is PaymentVoucher & 1 refere jam company ID
            _oVoucher.BaseCurrencyId = 1; //jas......this code is temporary 
            _oVoucher.CompanyID = 1;//jas......this code is temporary 
            _oVoucher.VoucherTypeID = 2;//jam for temporary basis code 2 is paymenttypeid that is payment voucher
            _oVoucher.CurrencyId = 1;
            _oVoucher.BaseCurrencyNameSymbol = "Taka[Tk]"; //jas......this code is temporary
            _oVoucher.VoucherDetailLst = VoucherDetail.Gets(_oVoucher.VoucherID);
            _oVoucher.LstCurrency = Currency.Gets();
            _oVoucher.Operation = "AddPaymnetVoucher";
            _oVoucher.DebitAccountHeadName = "Press Enter";
            _oVoucher.CreditAccountHeadName = "Press Enter";
            return Json(_oVoucher);
        }
        catch (Exception ex)
        {
            return Json(ex.Message);
        }
    }