CodeManiac
CodeManiac

Reputation: 1014

model binding to date picker jquery UI in asp.net mvc 3

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

Upvotes: 1

Views: 2660

Answers (1)

Kapil Khandelwal
Kapil Khandelwal

Reputation: 16144

Try using this:

<script>
    $(function() {
        $("#BirthDate").datepicker();
    });
</script>

Upvotes: 1

Related Questions