【发布时间】:2018-11-30 00:16:39
【问题描述】:
我在很多论坛上阅读过很多时候模型不会绑定到控制器,因为操作的参数具有相同的字段名称,但我将其更改为唯一名称,但我仍然没有在做了。 我在从 html 页面获取对象并使用 JSON.Stringify 对其进行序列化后传递对象,如果我在 chrome 工具中对其进行调试,“有效负载请求”中的一切看起来都很完美,但一旦它到达控制器,模型就为空.代码有什么问题?
型号:
public class JsonFileModel
{
public string[] Geo { get; set; }
public string[] State { get; set; }
public string[] Variables { get; set; }
public int[] Weights { get; set; }
public string[] Variable_Category { get; set; }
public string UID { get; set; }
}
AJAX 调用:
var geo_graphic_level = $('input[name=geographic-radio-name]:checked').val();
var state = $('#states-select-id option:selected').val();
var v = $('#variable-list-select-id option:selected');
var variables = [];
var json = "'" + JSON.stringify(variables) + "'";
$(v).each(function (index, v) {
variables.push($(this).val())
});
var variable_category = $('#categories-select-id option:selected').val();
var weights = [];
$("input[name='weight-name']").each(function () {
weights.push(this.value);
});
//上面的代码给出了对象的值
$.ajax({
type: "POST",
url: '@Url.Action("Save", "Drive")',
dataType: "json",
data: JSON.stringify({
Geo: geo,
State: state,
Variables: variables,
Weights: weights,
Variable_Category: variable_category
}),
contentType: 'application/json',
success: function (result) {
console.log(result);
},
error: function (result) {
}
});
控制器:
[HttpPost]
public JsonResult Save([FromBody] JsonFileModel d)
{
//code code
return Json("worked");
}
【问题讨论】:
标签: c# ajax asp.net-web-api http-post model-binding