Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript将null传递给C#控制器参数中的ExpandooObject字段_Javascript_C#_Asp.net Mvc_Json.net_Expando - Fatal编程技术网

Javascript将null传递给C#控制器参数中的ExpandooObject字段

Javascript将null传递给C#控制器参数中的ExpandooObject字段,javascript,c#,asp.net-mvc,json.net,expando,Javascript,C#,Asp.net Mvc,Json.net,Expando,我的Javascript代码 $('[step="4"]').click(function () { //shortened for brevety var _model = new Object(); _model.ItemDesc.value = 'Descript'; //^ throws an error but gets fixed if removing the .value _model.ItemQty.num = 1; _model.ItemQty.unit = 'pcs'

我的Javascript代码

$('[step="4"]').click(function () {
//shortened for brevety
var _model = new Object();
_model.ItemDesc.value = 'Descript';
//^ throws an error but gets fixed if removing the .value
_model.ItemQty.num = 1;
_model.ItemQty.unit = 'pcs'

    $.ajax({
        type: "POST",
        url: 'CreateItemCallAsync',
        data: _model,
        success: function (msg) {
            status = JSON.stringify(msg);
            alert('Item created successfully!');
            location.reload();
        },
        error: function (msg) {
            status = JSON.stringify(msg);
            alert('Failed to create item.');
            location.reload();
        }
    });
});
C#控制器代码

[HttpPost]
public async Task<JsonResult> CreateItemCallAsync(CreateItemModel item)
{
   //breakpoint here 
   var test = item.ItemDesc;
   var qty = item.ItemQty.num; //getting nulls here
   var unit = item.ItemQty.unit; //getting nulls here
}
JavaScript对象

[
  {
     ItemName : 'Item1',
     ItemDesc : 'Descript'
     ItemQty : { num : 5 , unit: 'pcs'}
  },
  {
     ItemName : 'Item2',
     ItemDesc : 'Descript'
     ItemQty : { num : 1 , unit: 'box'}
  }
]
从上面的代码。我有一个JavaScript对象通过
CreateItemModel
参数传递给我的C#控制器,该参数的
ItemQty
字段作为
ExpandoObject
。但是,在传递给我的C#controller之后。
ItemQty.num
ItemQty.unit
null

在将JavaScript对象传递给C#控制器之前,需要进一步研究。对象已成功填充

我需要
ItemQty
作为一个
expandooobject
,因为
ItemQty
下的字段/属性总是在变化/动态的

问题:

  • (有点离题)为什么
    \u model.ItemDesc.value='Descript'
    错误?另一方面,
    \u model.ItemDesc='Descript'
    运行时没有错误
  • 为什么我在
    ItemQty
    properties中得到空值
  • (有点离题)为什么
    \u model.ItemDesc.value='Descript'
    错误?另一方面,
    \u model.ItemDesc='Descript'
    运行时没有错误

    因为原始javascript
    对象中没有属性
    ItemDesc
    ItemQty
    ItemQty

    您可以尝试为javascript代码创建匿名JSON对象

    var _model = {     
        ItemDesc: {
            value : "Descript"
        }, 
        ItemQty :{
            num : 1,
            unit :'pcs'
        }
    };
    
    而不是

    var _model = new Object();
    _model.ItemDesc.value = 'Descript';
    _model.ItemQty.num = 1;
    _model.ItemQty.unit = 'pcs'
    
    您的c#模型可能看起来像,因为您当前的
    ItemDesc
    是一个对象,而不是字符串值

    为什么我在
    ItemQty
    属性中得到
    null

    因为默认ModelBindiner无法使用
    JSON
    key
    ItemQty
    对象找到
    ExpandoObject

    public class ItemDesc
    {
        public string value { get; set; }
    }
    
    public class ItemQty
    {
        public int num { get; set; }
        public string unit { get; set; }
    }
    
    public class CreateItemModel
    {
        public ItemDesc ItemDescContext { get; set; }
        public ItemQty ItemQtyContext { get; set; }
    }
    

    您可以演示如何使用您的接收
    JSON
    数据吗?将您的
    CreateItemModel
    添加到ajax请求中的问题更改数据:\模型为数据:{item:JSON.stringify(\模型)}由于您的api端点期望点1的item objectFor-
    ItemDesc
    是类型化的
    string
    -它没有名为
    value
    的属性,如果您想使用字典(使用默认的
    contentType
    ,那么它将是
    var\u模型={ItemDesc.value:'Descript',ItemQty[0]。键:'num',ItemQty[0]。值:1,ItemQty[1]。键:'unit',ItemQty[1]。值:'pcs'};
    public class ItemDesc
    {
        public string value { get; set; }
    }
    
    public class ItemQty
    {
        public int num { get; set; }
        public string unit { get; set; }
    }
    
    public class CreateItemModel
    {
        public ItemDesc ItemDescContext { get; set; }
        public ItemQty ItemQtyContext { get; set; }
    }