我是初学MVC5的编程人员。 我与MVC5和Entity Framework 6 工作,我有一个类和一个DateTime属性是这样的:为什么总是在数据库中,DateTime属性为空?
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}",ApplyFormatInEditMode = true)]
[Column(TypeName = "datetime2")]
public DateTime? BirthDay { get; set; }
我的控制器:
public ActionResult Create([Bind(Include = "MelliCode,EnCode,grade,quota,password,FName,Lname,shenasname,FatehrName,sex,tel,mobile,ResAddress,WorkAddress")] Expert expert)
{
if (ModelState.IsValid)
{
db.Experts.Add(expert);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(expert);
}
和我的看法是这样的:
<div class="form-group">
@Html.LabelFor(model => model.BirthDay, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BirthDay, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BirthDay, "", new { @class = "text-danger" })
</div>
但是,在页面运行后输入数据库中的个人信息插入为null生日!!!为什么?
请添加您的形式,控制层和数据层的代码了。 – Sefa
你是否将所有其他字段放入数据库? – Emil
是的,所有数据都是正确的,除了生日 –