1

i 'm beginner in programming with MVC5. I am working with MVC5 and Entity framework 6. I have a class and a DateTime property like this:

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}",ApplyFormatInEditMode = true)]
[Column(TypeName = "datetime2")]
public DateTime? BirthDay { get; set; }

my controller:

 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);
    }

and my view like this:

<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>

However, after the run of the page and enter personal information in the database is inserted to null for Birthday!!!why?

1 Answers1

0

I found my problem I had forgotten Birthday in [Bind] attribute

public ActionResult Create([Bind(Include = "BirthDay,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");
        }