[英]Passing data collection object from Jquery ajax Method to MVC Controller showing null object
I'm try to pass data object to MVC controller using JQuery method. 我尝试使用JQuery方法将数据对象传递给MVC控制器。 While I'm click submit button it will create data object in JavaScript method. 当我单击提交按钮时,它将以JavaScript方法创建数据对象。 Data Object is not null in Ajax method. 在Ajax方法中,数据对象不为null。 When we passing it to MVC controller it hit the method correctly but data object is showing null. 当我们将其传递给MVC控制器时,它正确命中了该方法,但数据对象显示为空。
I tried single object as well, but it also showing null. 我也尝试了单个对象,但它也显示为null。
This is JQuery Ajax Method: 这是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);
}
});
});
});
This is my MVC COntroller Method. 这是我的MVC COntroller方法。
[HttpPost]
public JsonResult Create(ExchangeRateSetUp model)
{
return Json("Success", JsonRequestBehavior.AllowGet);
}
Please help me to Sortout this issue. 请帮助我解决此问题。 Thanks. 谢谢。
You are passing data as json
string, hence you need to accept it as string in create
method. 您将数据作为json
字符串传递,因此您需要在create
方法中将其作为字符串接受。
Change ExchangeRateSetUp
object to String
parameter in create method and then covert String
to ExchangeRateSetUp
object : 在创建方法中将ExchangeRateSetUp
对象更改为String
参数,然后将String
隐藏为ExchangeRateSetUp
对象:
public JsonResult Create(String model)
{
//convert String model into ExchangeRateSetUp here
return Json("Success", JsonRequestBehavior.AllowGet);
}
same thing happened for me. 我也发生了同样的事情。 the reason was the absence of { get; 原因是缺少{get; set; 组; } in the c# class defined to accept model in controller. 在c#类中定义为在控制器中接受模型。
Check whether your class ExchangeRateSetUp has { get; 检查您的类ExchangeRateSetUp是否具有{get; set; 组; } for the properties }属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.