【发布时间】:2019-08-14 21:15:38
【问题描述】:
我在我的 MVC 5 应用程序中遇到了一个奇怪的问题,我想使用 Ajax 帖子将 Drop Down Selected 值传递给控制器。
邮政编码如下所示:
$(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 工作正常并正确显示所选值。但是在控制器中,字符串显示为 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())},
这也行不通。任何帮助都感激不尽。谢谢。
【问题讨论】:
-
在 BUName 上不加引号应该可以工作
-
不需要对字符串进行字符串化:
data: {"BUName": $('#Lob option:selected').text()},- 但你应该在 BUName 中得到一些东西,所以其他地方有问题。 -
为什么是 JSON 类型?你正在传递一个字符串
-
我觉得“contentType: JSON”也不合适
-
不是问题,但值得一提:使用
url: '@Url.Action("GetValueStreams", "Dashboard")',
标签: c# jquery ajax asp.net-mvc asp.net-ajax