【发布时间】:2017-05-25 05:07:45
【问题描述】:
这个 ajax 从 .cshtml 文件调用到控制器,但 getValue 函数在加载时采用 Null 值,但之后它工作正常。它甚至在 ajax 调用上显示警报值,但在控制器上显示为空。任何帮助将不胜感激。
<script>
$(document).ready(function(){
//get value from dropdown list
var ID = $('#cmb').val();
// ID is taking value
$.ajax({
// /ControllerName/FunctionName
// /Home/getvalue
url: "/Home/getValue",
type: "POST",
// ID is getting from dropdownList
// parameterName/value
data: JSON.stringify({'_ID':ID}),
dataType: "json",
traditional: true,
contentType: "application/json; charset=utf-8",
success: function (data) {
if (data.status == "success") {
//if got response
//alert(data.status);
}
},
error: function () {
//if if not response
//alert('something went wrong');
}
});
});
</script>
[HttpPost]
public JsonResult getValue(string _ID)
{
string a = _ID;
}
【问题讨论】:
-
数据:'{"_ID": ' + '"' + ID + '"}'
-
可能您的 ID 没有价值。或尝试使用:data:{_ID : ID}
标签: c# asp.net-mvc asp.net-ajax asp.net-mvc-5