dimanche 30 août 2015

How to retrieve the datetime selected from datetimePicker from database in the edit section in Asp.Net MVC 3?

I want to retrieve the datetime selected at the time of create i.e date of birth in this date. I have used datetime picker for picking date from the calender.

Controller:

public ActionResult EditDependent(decimal id)
{
    if (Session["EMPLOYEEID"] == null)
        return RedirectToAction("Index");           

    Dependent dependent = db.Dependent.Find(id);
    //ViewBag.RelationData = new SelectList(db.Relation.OrderBy(r => r.Description), "Id", "Description");
    ViewBag.RelationData = new SelectList(db.Relation.ToList(), "Id", "Description",dependent.RelationId);
    ViewBag.Occupation = new SelectList(db.Occupation.ToList(), "Id", "Description",dependent.OccupationId);
    return View(dependent);
}

[HttpPost]
public ActionResult EditDependent(Dependent dependent)
{
    if (Session["EMPLOYEEID"] == null)
        return RedirectToAction("Index");  
    if (ModelState.IsValid)
    {
        db.Entry(dependent).State = EntityState.Modified;
        PersonalInformationSystem.UpdateDependent(dependent.Id, Convert.ToInt32(Session["EMPLOYEECODE"]), dependent.Name,
                Convert.ToInt32(dependent.RelationId),Convert.ToDateTime(dependent.DOB), Convert.ToInt32(dependent.OccupationId), User.Identity.Name.ToString());
        return RedirectToAction("DependentList/" + Session["EMPLOYEEID"]);
    }
    return View(dependent);
}

Views:

<td>                   
  <div class="editor-label">
    @Html.LabelFor(model => model.DOB, "Date Of Birth")
  </div>
</td>
<td>
  <div class="editor-field">
    @Html.EditorFor(model => model.DOB)
        @Html.ValidationMessageFor(model => model.DOB)
  </div>
</td>

Model: public class Dependent { [Key] public decimal Id { get; set; } public string Name { get; set; } //[ForeignKey("RelationId")] public int RelationId { get; set; } [DataType(DataType.Date)] [Display(Name = "Event Date")] [DisplayFormat(DataFormatString = "{0:d}")] public DateTime DOB { get; set; }

    public int OccupationId{ get; set; }
    public int EmployeeCode { get; set; }
    public decimal EmployeeId { get; set; }
    public string EntryBy { get; set; }
     [DataType(DataType.Date)]
    [Display(Name = "Event Date")]
    [DisplayFormat(DataFormatString = "{0:d}")]
    public DateTime LastModifiedDate { get; set; }

Aucun commentaire:

Enregistrer un commentaire