簡體   English   中英

將數據收集對象從Jquery ajax方法傳遞到顯示空對象的MVC控制器

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM