当Ajax传递空值时,控制器在ASP.NET MVC中接收到null的问题可以通过以下方法解决:
$.ajax({
url: '/Controller/Action',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({ paramName: null }),
success: function(response) {
// 处理响应数据
},
error: function(xhr, status, error) {
// 处理错误
}
});
[HttpPost]
public ActionResult ActionName([FromBody]Nullable paramName)
{
// 处理接收到的数据
return Json(result);
}
这样,在Ajax请求中传递空值时,控制器接收到的参数将会是null,而不是默认值或空字符串。
请注意,以上示例中的Controller和ActionName需要根据实际情况进行替换。