繁体   English   中英

数据是通过AJAX传递给操作的对象数组

[英]Data is array of objects passed to action through AJAX

在这里,我可以将数据作为对象数组通过AJAX传递给控制器​​,但是我在控制器端获得的值为null。

var newData  = [{Code:"FT",Id:1:Year:2005,TypeId:1,Value:20},
                {Code:"FR",Id:2:Year:2006,TYpeId:3,Value:40},
                {Code:"FY",Id:3:Year:2007,TYpeId:5,Value:50}]


$.ajax({
    url: "api/FG/cretejson",
    type: 'POST',
   contentType: "application/json",
   data: JSON.stringify({ extraParams: newData }),
   success: function (data) {
       var result = result;
   }
});

public JsonResult cretejson([FromBody]List<rev> extraParams)
{
    try
    {

        return Json(new { Result = "OK", Options = extraParams });
    }
    catch (Exception ex)
    {
        return Json(new { Result = "ERROR", Message = ex.Message });
    }
}
public class rev
{
    public string Id { get; set; }
    public string Code { get; set; }
    public int TypeId { get; set; }
    public int Year { get; set; }
    public int Value { get; set; }
}

任何帮助,将不胜感激。

AJAX通话:

 $.ajax({
       url: urlString,
       type: 'POST',
       data: sc,
       dataType: 'json',
       crossDomain: true,
       cache: false,
       success: function (data) { console.log(data); }
    });

Web API控制器:

 [HttpPost]
 public string PostProducts([FromBody]List<SyncingControl> persons)
 {
    return persons.Count.ToString(); // 0, expected 3
 }

暂无
暂无

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

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