使用参数从javascript调用控制器结果类型操作
我尝试以下方法: 我的行动是:使用参数从javascript调用控制器结果类型操作,javascript,ajax,asp.net-mvc-3,Javascript,Ajax,Asp.net Mvc 3,我尝试以下方法: 我的行动是: [HttpPost] public ActionResult PaymentVoucherCommit(string sParameter) { try { _oVoucher = new Voucher(); _oVoucher = _oVoucher.CommitVoucherNo(2, 1); // Here 2 r
[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]请求,我已成功完成相同类型的任务。但是我为post类型操作(操作结果)创建了一个三元组。请简单尝试:
$('#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);
}
}
从操作返回的数据类型不是json。它是视图(字符串)。如果要返回要在javascript中使用的操作结果,步骤是什么?查看从操作返回的凭证数据,或从操作返回的凭证数据?在成功回调函数中将包含的邮政编码