2012-10-08 36 views
0

我嘗試以下方法:呼叫控制器Actinresult類型的動作與參數

我的行動是:

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

請修復這個bug。

注意:我已成功完成[httpGet]請求的相同類型的任務。但我爲崗位類型的動作(操作結果)黑社會

+0

從你的行動返回的數據類型是不是JSON。它是視圖(字符串)。 – karaxuna

+0

如果要返回行動結果,步驟是什麼 – csefaruk

+0

您想在JavaScript中使用什麼?從操作返回的視圖,還是從操作返回的憑證數據? – karaxuna

回答

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