C# 可为Null的对象必须具有值ASP.NET MVC日期时间选择器

C# 可为Null的对象必须具有值ASP.NET MVC日期时间选择器,c#,jquery,asp.net-mvc,datetimepicker,C#,Jquery,Asp.net Mvc,Datetimepicker,在将数据保存到控制器中时,我的视图为什么不将数据绑定到模型中,这真是令人困惑。我使用Jquery日期时间选择器创建了这个UI。“提交”按钮消息发出后,“可空对象必须有一个值”尝试调试,此错误将显示在“日期开始”和“日期结束”视图中,因为这些数据从未绑定(无值/null)到模型中 基于该UI,我想使用EF将这些数据保存到sql server数据库中。我的型号是Datetime类型 我的模型: public class OvertimeRequestModel { public int O

在将数据保存到控制器中时,我的视图为什么不将数据绑定到模型中,这真是令人困惑。我使用Jquery日期时间选择器创建了这个UI。“提交”按钮消息发出后,“可空对象必须有一个值”尝试调试,此错误将显示在“日期开始”和“日期结束”视图中,因为这些数据从未绑定(无值/null)到模型中

基于该UI,我想使用EF将这些数据保存到sql server数据库中。我的型号是Datetime类型

我的模型:

public class OvertimeRequestModel
{
    public int OvertimeId { get; set; }
    public string OvertimeCode { get; set; }
    public DateTime? DateStart { get; set; } //datetime format
    public DateTime? DateEnd { get; set; } //datetime format
    public string Notes { get; set; }
}
我在视图中使用了texboxfor:

 <div class="input-group col-xs-10 col-sm-5">
    @Html.TextBoxFor(m => m.DateStart, @"{0:MM/dd/yyyy HH:mm:ss}", new { @class = "form-control", @id = "date-timepicker1", @placeholder = "select date start", required = "required", @autocomplete = "off" })
    <span class="input-group-addon">
        <i class="fa fa-clock-o bigger-110"></i>
    </span>
</div>

 <div class="input-group col-xs-10 col-sm-5">
    @Html.TextBoxFor(m => m.DateEnd, @"{0:MM/dd/yyyy HH:mm:ss}", new { @class = "form-control", @id = "date-timepicker2", @placeholder = "select date End", required = "required", @autocomplete = "off" })
    <span class="input-group-addon">
        <i class="fa fa-clock-o bigger-110"></i>
    </span>
</div>
需要建议,谢谢。

查看

 @Html.TextBoxFor(model => model.StartDate, "{0:dd.MM.yyyy}", new { @class = "date-picker fullwidth", data_date_format = "dd.mm.yyyy" })
Js

模型


最后,我有一个小技巧来解决这个问题。只需将我的数据类型从datetime更改为string。问题是因为html助手无法识别要存储到控制器中的日期时间格式

public string DateStart { get; set; } 
public string DateEnd { get; set; } 
查看与上面发布的相同的内容:

@Html.TextBoxFor(m => m.DateEnd, @"{0:MM/dd/yyyy HH:mm:ss}", new { @class = "form-control", @id = "date-timepicker2", @placeholder = "select date End", required = "required", @autocomplete = "off" })
保存此数据的控制器为:

OvertimeRequest over = new OvertimeRequest();
over.OvertimeCode = model.OvertimeCode;
over.DateStart = DateTime.ParseExact(model.DateStart, "MM/dd/yyyy HH:mm:ss", null); //i think this is the most important thing
over.DateEnd = DateTime.ParseExact(model.DateEnd, "MM/dd/yyyy HH:mm:ss", null); //i think this is the most important thing
over.Notes = model.Notes;
db.OvertimeRequest.Add(over);
int result = db.SaveChanges();

最后一切顺利

如果不使用
日期时间选择器
,会发生什么情况?为了确保它不会干扰绑定,请尝试删除它并检查绑定是否工作。如果它是必填字段,为什么它可以为空?@RonBeyer这只是实体框架的反映。@PriyankPanchal当我试图删除
datetimepicker
时,我仍然得到空值
。DateStart
-这是否可以作为
模型的空值。DateStart
?如果没有,则必须指定值,而不是像
model.DateStart.value
那样的对象。或者让日期对象在实体模型中也可以为空。您还可以进行一些空检查,如
if(model.DateStart.Hasvalue)
,然后进行相应的赋值。如果用户希望将此字段留空,它如何将
null
存储在数据库中?如果字段为空,用户不应该在其中看到任何默认日期,对吗?
public DateTime StartDate { get; set; }
public string DateStart { get; set; } 
public string DateEnd { get; set; } 
@Html.TextBoxFor(m => m.DateEnd, @"{0:MM/dd/yyyy HH:mm:ss}", new { @class = "form-control", @id = "date-timepicker2", @placeholder = "select date End", required = "required", @autocomplete = "off" })
OvertimeRequest over = new OvertimeRequest();
over.OvertimeCode = model.OvertimeCode;
over.DateStart = DateTime.ParseExact(model.DateStart, "MM/dd/yyyy HH:mm:ss", null); //i think this is the most important thing
over.DateEnd = DateTime.ParseExact(model.DateEnd, "MM/dd/yyyy HH:mm:ss", null); //i think this is the most important thing
over.Notes = model.Notes;
db.OvertimeRequest.Add(over);
int result = db.SaveChanges();