简体   繁体   English

将数据收集对象从Jquery ajax方法传递到显示空对象的MVC控制器

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

相关问题 mvc控制器从具有数据的javascript对象传入null - mvc controller is passing in null from javascript object that has data Jquery ajax将空值传递给MVC控制器 - Jquery ajax passing null values to MVC controller jQuery AJAX发布到MVC Controller对象-请求显示为null - jQuery AJAX post to MVC Controller object — request shows up null 数据未从jquery ajax调用传递到我的mvc4控制器中的actionresult方法中 - Data not passing into actionresult method in my mvc4 controller from a jquery ajax call JQuery将'null参数传递给MVC控制器方法 - JQuery passing 'null parameters to MVC controller method ASP.NET Core 中的 MVC 控制器的 AJAX POST 数据在传递超过一定长度的对象数组时始终为空 - AJAX POST Data to MVC Controller in ASP.NET Core Always Null When Passing An Object Array Over a Certain Length ASP.NET MVC通过Java序列化到Controller的AJAX通过BeginCollectionItem List的对象传递null - ASP.NET MVC Passing a BeginCollectionItem List's Object through AJAX with Javascript Serialize to Controller is passing null 使用jquery和ajax将json对象发布到mvc控制器 - Post a json object to mvc controller with jquery and ajax 从ajax作为null发送到MVC控制器的数据 - Data sent from ajax as null into the MVC controller 使用jQuery Ajax将单个对象传递到MVC控制器方法 - Passing A Single Objects Into An MVC Controller Method Using jQuery Ajax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM