![](/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.