MVC 单字符串 JSON post 总是在控制器中出现 Null
MVC Single String JSON post always coming Null in Controller
我在 MVC 5 应用程序中遇到一个奇怪的问题,我想使用 Ajax post.
将下拉选择值传递给控制器
Post 代码如下所示:
$(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
});
}
});
});
});
ALERT 工作正常并正确显示所选值。但是在控制器中,该字符串显示为空。
[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
}
});
我在 MVC 5 应用程序中遇到一个奇怪的问题,我想使用 Ajax post.
将下拉选择值传递给控制器Post 代码如下所示:
$(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
});
}
});
});
});
ALERT 工作正常并正确显示所选值。但是在控制器中,该字符串显示为空。
[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
}
});