![](/img/trans.png)
[英]mvc controller is passing in null from javascript object that has data
[英]Passing data collection object from Jquery ajax Method to MVC Controller showing null object
我尝试使用JQuery方法将数据对象传递给MVC控制器。 当我单击提交按钮时,它将以JavaScript方法创建数据对象。 在Ajax方法中,数据对象不为null。 当我们将其传递给MVC控制器时,它正确命中了该方法,但数据对象显示为空。
我也尝试了单个对象,但它也显示为null。
这是JQuery Ajax方法:
$(function () {
$('.save-user').on('click', function () {
var tr = $(this).parents('tr:first');
var SrcCountryId = $("#ExchangeRateSetUpHeader_SrcCountryId option:selected").val();
var DestCountryId = $("#ExchangeRateSetUpHeader_DestCountryId option:selected").val();
var SrcCurrencyId = $("#ExchangeRateSetUpHeader_SrcCurrencyId option:selected").val();
var DestCurrencyId = $("#ExchangeRateSetUpHeader_DestCurrencyId option:selected").val();
var Rate = $("#ExchangeRateSetUpHeader_Rate").val();
var RemittanceSettlementId = tr.find("#RemittanceId").val();
var CommPercentage = tr.find("#CommPercentage").val();
var CommFixed = tr.find("#CommFixed").val();
var SellRate = tr.find("#SellRate").val();
var Id = tr.find("#ItemId").val();
var ExchangeRateSetUp =
{
"Id": Id,
"SrcCountryId": SrcCountryId,
"DestCountryId": DestCountryId,
"SrcCurrencyId": SrcCurrencyId,
"DestCurrencyId": DestCurrencyId,
"Rate": Rate,
"RemittanceSettlementId": RemittanceSettlementId,
"CommPercentage": CommPercentage,
"CommFixed": CommFixed,
"SellRate": SellRate
};
$.ajax({
url: '/ExchangeRateSetUp/Create/',
type: 'POST',
data: JSON.stringify(ExchangeRateSetUp),
contentType: 'application/json; charset=utf-8',
success: function (data) {
alert(data);
}
});
});
});
这是我的MVC COntroller方法。
[HttpPost]
public JsonResult Create(ExchangeRateSetUp model)
{
return Json("Success", JsonRequestBehavior.AllowGet);
}
请帮助我解决此问题。 谢谢。
您将数据作为json
字符串传递,因此您需要在create
方法中将其作为字符串接受。
在创建方法中将ExchangeRateSetUp
对象更改为String
参数,然后将String
隐藏为ExchangeRateSetUp
对象:
public JsonResult Create(String model)
{
//convert String model into ExchangeRateSetUp here
return Json("Success", JsonRequestBehavior.AllowGet);
}
我也发生了同样的事情。 原因是缺少{get; 组; 在c#类中定义为在控制器中接受模型。
检查您的类ExchangeRateSetUp是否具有{get; 组; }属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.