我正在尝试将完整的表单和单独的字符串列表发布到我的 Controller 。表单绑定(bind)到模型,但字符串列表不绑定(bind)到 Controller 中的字符串列表,并在列表中显示为单个字符串。
var List = "one,two,three";
var dataToPost = $('#Form').serialize() + "&Words=" + JSON.stringify(List);
$.ajax({
type: "POST",
url: '/Home/Open/',
data: dataToPost,
dataType: "json",
success: function () {
alert('ok');
},
error: function () {
alert('error');
}
});
这是我的 MVC Controller 操作:
[HttpPost]
public IActionResult Open(DataModel Model, List<string> Words)
{
return View(Model);
}
最佳答案
这对我来说效果很好:
var List = ["one", "two", "three"];
var dataToPost = $('#Form').serializeArray();
List.forEach(function (value, index) {
dataToPost.push({ name: 'Words[]', value: value });
});
关于javascript - 将表单数据和字符串列表发送到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55734759/