ASP.NET 核心模型未通过 AJAX 绑定到控制器
ASP.NET Core model not binding to the controller via AJAX
我在很多论坛上读到,很多时候模型不会绑定到控制器,因为操作的参数与字段名称相同,但我将其更改为唯一的名称,我是仍然没有得到它。
我在从 html 页面获取对象并使用 JSON.Stringify 对其进行序列化后传递这些对象,如果我在 chrome 工具中调试它,那么在 'Payload request' 中一切看起来都很完美但是作为一旦它到达控制器,模型就为空。代码有什么问题?
型号:
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");
}
好吧,所以我错误地定义了我的属性,例如,当我只期望来自 Geo 的字符串时,我将我的模型定义为期望 [] 数组。修复后,一切正常!
我在很多论坛上读到,很多时候模型不会绑定到控制器,因为操作的参数与字段名称相同,但我将其更改为唯一的名称,我是仍然没有得到它。 我在从 html 页面获取对象并使用 JSON.Stringify 对其进行序列化后传递这些对象,如果我在 chrome 工具中调试它,那么在 'Payload request' 中一切看起来都很完美但是作为一旦它到达控制器,模型就为空。代码有什么问题?
型号:
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");
}
好吧,所以我错误地定义了我的属性,例如,当我只期望来自 Geo 的字符串时,我将我的模型定义为期望 [] 数组。修复后,一切正常!