在我的按钮上单击我将模型传递给带有datetime对象的控制器,在我的本地数据库中,它工作正常但是当我上传到服务器时,datetime值正在使用null.how来解决此问题。 这是我点击按钮的代码
$('#btnTaxiCall').on('click', function () {
stopCallTime();
var TaxiCallTime = new Date();
//alert(TaxiCallTime.toUTCString());
// $('#TaxiCallTime').val(TaxiCallTime.toISOString());
$('#TaxiCallTime').val($.date(TaxiCallTime));
var model = $("#formTaxiLog").serializeArray();
$('#btnTaxiArrival').removeAttr('disabled');
$.ajax({
url: '/TaxiLog/Add', type: 'Post', dataType: 'json',
data: $.param(model),
success: function (data) { $('#btnTaxiCall').css({ backgroundColor: 'green' }); isSaved = true; Id = data.id; $('#CallTime button').attr('disabled', 'disabled'); $('#ErrMsg').hide(); $('#CallTime div').removeClass('customValidation'); $("#Id").val(Id); }
});
$.date = function (dateObject) {
// alert(dateObject);
var d = new Date(dateObject);
var day = d.getDate();
var month = d.getMonth();
var year = d.getFullYear();
var hours = d.getHours();
var minutes = d.getMinutes();
var seconds = d.getSeconds()
var date = day + "/" + month + "/" + year + " " + hours + ((minutes < 10) ? ":0" : ":") + minutes + ((seconds < 10) ? ":0" : ":") + seconds;
return date;
}
public int Post(TaxiLogVM vm)
{
using (var dc = new SGSDBContext())
{
string sql = "SELECT COUNT(Id) FROM [dbo].[Venue] WHERE [Id]={0};";
if (dc.Database.SqlQuery<int>(sql, vm.VenueId).FirstOrDefault() == 0)
{
return 0;
}
else
{
vm.CreatedDate = vm.ModifiedDate = DateTime.Now;
//vm.TaxiArrivalTime = null;
sql = "INSERT INTO [dbo].[TaxiLog]([VenueId],[UserId],[Title],[Name],[Destination],[TaxiFirmId],[TaxiCallTime],[TaxiArrivalTime],[CreatedDate],[ModifiedDate]) "
+ "VALUES({0},{1},{2},{3},{4},{5},{6},{7},{8},{9}); "
+ "SELECT SCOPE_IDENTITY();";
vm.Id = (int)dc.Database.SqlQuery<decimal>(sql, vm.VenueId, vm.UserId, vm.Title, vm.Name, vm.Destination, vm.TaxiFirmId, vm.TaxiCallTime.Value.ToString(" hh:mm:ss tt"), SqlDateTime.Null, vm.CreatedDate, vm.ModifiedDate).FirstOrDefault();
dc.SaveChanges();
return vm.Id;
}
}
}
答案 0 :(得分:0)
不应将datetime
传递给控制器,而应将其作为string
传递,然后解析为控制器中的datetime
。
创建一个将日期时间转换为字符串的字符串属性。
public string MyDateTimeString
{
get{ return datetime.toString();}
set{ datetime = Convert.toDateTime(value);}
}