我在 MVC 5 应用程序中遇到一个奇怪的问题,我想使用 Ajax post 将下拉选定值传递给 Controller 。
邮政编码如下所示:
$(function () {
//Change the Value stream list each time the BU is selected
$('#Lob').change(function () {
alert(JSON.stringify($('#Lob option:selected').text()));
$.ajax({
url: '@Url.Content("~/Dashboard/GetValueStreams/")',
dataType: 'json',
type: 'POST',
data: JSON.stringify($('#Lob option:selected').text()),
contentType: 'application/json',
success: function (VSList) {
// do stuff
});
}
});
});
});
警报工作正常并正确显示所选值。但是在 Controller 中,该字符串显示为 null。
[HttpPost]
public ActionResult GetValueStreams(string BUName)
{
// Here the BUName parameter is coming as null.
}
我尝试将 JSON POST 数据更改为以下内容:
data: {"BUName": JSON.stringify($('#Lob option:selected').text())},
这也行不通。任何帮助都感激不尽。谢谢。
最佳答案
将数据更改为data: JSON.stringify({BUName : $('#Lob option:selected').text()})
。
我测试过,它有效。
$.ajax({
url: '@Url.Content("~/Dashboard/GetValueStreams/")',
dataType: 'json',
type: 'POST',
data: JSON.stringify({BUName : $('#Lob option:selected').text()}),
contentType: 'application/json',
success: function (VSList) {
// do stuff
}
});
关于c# - MVC 单字符串 JSON 帖子在 Controller 中始终为 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55324104/