所以我有一个像这样的视图模型
public class TestViewModel
{
public DateTime? StartDate { get; set; }
public DateTime StartDate2 { get; set; }
}
使用该视图模型,我希望如果我通过 ajax 的 JSON 向这些属性发送“NULL”,那么这些属性的值是StartDate = null和StartDate2 = {01/01/0001 00.00.00}
这在我的本地机器上按预期工作,但在我的服务器上它返回错误Bad Request
我知道我的服务器返回Bad Request是因为模型绑定的事情
为什么我的服务器与本地机器的行为不同?我该如何解决这个问题?
编辑 :
顺便说一句,如果我将StartDateandStartDate2类型更改为string. 它没有错误,代码完美运行
这是我在控制器中的操作
public ActionResult TestAction(List<TestViewModel> p_viewModel)
{
//The code actually does not reach here, because it fail on model binding ?
// bla bla bla some logic here
}
这是我的 javascript
function getData()
{
let datas = [];
$("#tableListMeeting tbody tr").each(function (i, row) {
let rowItem = $(row);
let startDate = rowItem.find(`input[name$=".StartDate"]`).val();
//change date format
startDate = (startDate == "") ? null : moment(startDate, "DD-MM-YYYY").format("YYYY/MM/DD");
let item = {
StartDate: startDate
};
datas.push(item);
});
return datas;
}
function onButtonClick(){
let data = getData();
let URL_TO_ACTION = "......"; //url to my action
$.ajax({
url: URL_TO_ACTION,
type: 'post',
datatype: 'json',
data: JSON.stringify(data),
contentType: 'application/json; charset=utf-8',
success: function (response) {
console.log("ajax local event 'sucess'");
console.log(response);
},
error: function (jqXHR, ajaxOptions, thrownError) {
if (jqXHR.status == 400) {
let a = jqXHR.responseText;
//logic to print error message
} else {
alert("Something went wrong, please try again");
}
},
beforeSend: function () {
console.log("ajax local event 'beforeSend'");
}
}