简体   繁体   English

NULL 值同时通过 ZA34A6659BCEAE779F28185E75Z7 将 JSON 数据传递给 controller

[英]NULL values while passing JSON data to controller via AJAX

Trying to pass following JSON from View to Controller试图将 JSON 从 View 传递到 Controller

{
    "allselected": false,
    "selectedIds": ["", "all"],
    "targetControl": "Studieniva",
    "targetSource": "studienivalist",
    "dependency": [{
        "name": "ShopNo",
        "selectedvalues": "311"
    }, {
        "name": "Institution",
        "selectedvalues": ["all", "UIO"]   // THIS IS COMING AS NULL AT CONTROLLER  . OCCURS ONLY WHEN MULTIPLE ITEMS UNDER dependency
    }]
}

Javascript code for sending request is like below (trying to make multiple requests at the same time and based on the response of each request loading some dropdowns ) Javascript 发送请求的代码如下(尝试同时发出多个请求,并根据每个请求的响应加载一些下拉菜单)

        var targetcontrols_array = targetControl.split(',');
        var targetsource_array = targetSource.split(',');
        const arrOfPromises = targetcontrols_array.map(function(item, index) {
            var control_name = item;
            var source = targetsource_array[index];
            var request_data = JSON.stringify({ allselected: allselected_flag, selectedIds: selectedvalues, targetControl: control_name, targetSource: source, dependency: dependencyOptions });
            console.log("req:"+request_data); //SAMPLE JSON ADDED On TOP
            return new Promise((resolve) => {
                $.ajax({
                    url: action_url,
                    type: 'POST',
                    traditional: true,
                    async: false,
                    data: request_data,
                    contentType: "application/json; charset=utf-8",
                    dataType: 'json',
                    success: function (response) {
                       //process response
                    }
                })
            });
        });
        Promise.all(arrOfPromises); 

MVC controller action is like below MVC controller 动作如下

 [HttpPost]
 public JsonResult CascadingDropDown(DynamicFormSelectModel model)
 {
     model.dependency  values are not coming correctly . Observing that the value of SelectedValues is NULL in some cases
     ( OCCURS ONLY WHEN MULTIPLE ITEMS UNDER dependency )
 }

public class DynamicFormSelectModel
{
    public bool allselected { get; set; }
    public string[] selectedIds { set; get; }
    public string targetSource { set; get; }
    public string targetControl { set; get; }
    public List<DynamicFormDependencyOptions> dependency { set; get; }

}
public class DynamicFormDependencyOptions
{
    public string Name { get; set; }
    public string SelectedValues { get; set; }
}

While trying to read the request model.dependency values, sometimes getting NULL, even if values are passed from View (Example attached JSON passing 2 values "name": "Institution", "selectedvalues": ["all", "UIO"], but in controller i am getting NULL While trying to read the request model.dependency values, sometimes getting NULL, even if values are passed from View (Example attached JSON passing 2 values "name": "Institution", "selectedvalues": ["all", "UIO"] ,但在 controller 我得到 NULL 在此处输入图像描述 . . OCCURS mostly WHEN MULTIPLE ITEMS are present under dependency property and no idea why its coming as NULL主要发生在依赖属性下存在多个项目并且不知道为什么它以 NULL 出现时

    public class DynamicFormDependencyOptions
{
    public string Name { get; set; }
    public string SelectedValues { get; set; }
}

change it to将其更改为

public class DynamicFormDependencyOptions
    {
        public string Name { get; set; }
        public string[] SelectedValues { get; set; }
    }

this code has the SelectedValues as string object but the data is in string Array form make SelectedValues a string Array and You'll get it.... and change your following code accordingly to parse the string array like you are doing for selectedIds .此代码将SelectedValues作为字符串 object 但数据采用字符串数组形式使 SelectedValues 成为字符串数组,您将得到它....并相应地更改以下代码以解析字符串数组,就像您为selectedIds所做的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM