我试图通过模型将datePicker输入的值传递给操作。但我总是得到这个值:
Date = {1/1/0001 12:00:00 AM}
我使用ajax来序列化并向操作发送输入值。我看到控制台,发现序列化表单时的sender值为true,但实际操作中的日期值是错误的。
html:
<input id="Fromdate" name="Fromdate" class="input-text full-width hasDatepicker" type="text">
型号:
public class CharterFlightSearchModel
{
public int Inf { get; set; }
public int Adt { get; set; }
public int Chd { get; set; }
public int depCountryId { get; set; }
public int DepCityId { get; set; }
public int ArrCountryId { get; set; }
public int ArrCityId { get; set; }
public int DateRange { get; set; }
public DateTime Fromdate { get; set; }
}
单击搜索按钮时调用此函数。jquery:
function ShowCharterFlight() {
$('#WrapCharterFlights').empty();
$('#WrapCharterFlights').html('<div class="col-xs-12 text-center"><img class="loadingimg" src="/Content/Travelo/images/travelenter_process.gif" /></div>');
$.ajax({
url: '/Charter/CharterPartialViewResult',
data: $("#CharterFlights").serialize(),
type: "POST",
success: function (result) {
$('#WrapCharterFlights').html(result);
$(".loadingimg").hide();
//allow = true;
},
error: function (jqXhr, textStates, errorThrown) {
alert(errorThrown);
$(".loadingimg").hide();
$('#WrapCharterFlights').html('<div class="text-center"><label class="text-danger btn btn-danger" style="font-size: 16px;">خطا در بارگذاری اطلاعات . لطفا دوباره امتحان کنید. </label></div>');
//allow = true;
}
});
}
不确定原因,但如果您使用jquery日期选择器https://jqueryui.com/datepicker/则服务器将正确地解析从客户端发送的所有日期。
jquery脚本文件的引用:
<script src="//code.jquery.com/jquery-1.10.2.js"></script> <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
HTML:
<input type="text" id="thedatepicker">
JQUERY初始化:
$(function() {
$( "#thedatepicker" ).datepicker({
changeMonth: true,
changeYear: true,
dateFormat: "yy-mm-dd"
});
});
您的函数(将日期选择器的值发送到POST请求中(这个日期选择器值显然需要验证)
function ShowCharterFlight() {
$('#WrapCharterFlights').empty();
$('#WrapCharterFlights').html('<div class="col-xs-12 text-center"><img class="loadingimg" src="/Content/Travelo/images/travelenter_process.gif" /></div>');
$.ajax({
url: '/Charter/CharterPartialViewResult',
data: {date:document.getElementById('startdatepicker').value},
type: "POST",
success: function (result) {
$('#WrapCharterFlights').html(result);
$(".loadingimg").hide();
//allow = true;
},
error: function (jqXhr, textStates, errorThrown) {
alert(errorThrown);
$(".loadingimg").hide();
$('#WrapCharterFlights').html('<div class="text-center"><label class="text-danger btn btn-danger" style="font-size: 16px;">خطا در بارگذاری اطلاعات . لطفا دوباره امتحان کنید. </label></div>');
//allow = true;
}
});
}