1

I have used jQuery UI Helpers for Date picker using following code.

 <div>
        <label for="date">Select a date:</label> @Html.JQueryUI().Datepicker("date")
    </div>

But How can I used this helper to model binding same as:

  <div class="editor-label">
        @Html.LabelFor(model => model.BirthDate)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.BirthDate)
        @Html.ValidationMessageFor(model => model.BirthDate)
    </div>

So, that value is saved to the database.

I have saved the post value of Birth date in database using Formcollection like below:

[HttpPost]
        public ActionResult Edit(User user,FormCollection PostData)
        {
            if (ModelState.IsValid)
            {
                db.Users.Attach(user);
                user.BirthDate = DateTime.Parse(PostData["date"]);
                UpdateModel(user);
                //db.ObjectStateManager.ChangeObjectState(user, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Details");
            }
            ViewBag.RoleID = new SelectList(db.Roles, "ID", "Name", user.RoleID);
            return View(user);
        }

1 Answer 1

1

Try using this:

<script>
    $(function() {
        $("#BirthDate").datepicker();
    });
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it shows datepicker. Now, Don't have to use FormCollection at controller
Hello Kapil, Here, DatePicker takes default time value (6/6/2012 12:00:00 AM) But I need to remove time and show only date. Ya, It can be done using ToShortDateString(). But How to use that function since I have @Html.DisplayFor(model => model.BirthDate) for displaying birthdate. I have also looked at database and it showing only date not time. what I have to do for showing only date. I am new to mvc 3 and really get confuse for single slight modification.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.