1

I want to set Bootstrap DateTimePicker value

 @using (Html.BeginForm("Index", "ObjectClass", FormMethod.Post, new { }))
{ 
    <div class="col-md-7">
        Du
        @Html.TextBoxFor(model => Model.helpVM.date1, new { @class = "dp datepicker", @value = DateTime.Now })
        Au
        @Html.TextBoxFor(model => Model.helpVM.date2, new { @class = "dp datepicker", @value = DateTime.Now })
}

I've tried this but it gives me no value in the view page.

3
  • instead of @value = ... try @Value = ... instead of small v use capital V Commented Feb 24, 2015 at 10:12
  • Set the values of helpVM.date1 and helpVM.date2 in the controller. Commented Feb 24, 2015 at 10:13
  • Value = DateTime.Now ,use like this Commented Feb 24, 2015 at 10:31

1 Answer 1

2

Change @value to Value.

@Html.TextBoxFor(model => model.helpVM.date1, new { @class = "dp datepicker", Value = DateTime.Now })
Sign up to request clarification or add additional context in comments.

4 Comments

And when you post and return the view - what ever date the user entered gets wiped out and reset to DateTime.Now!
Yes I agree i just wanted to simplify the code to share with you , i normaly send value via session variable.
@drexdrex, @Html.TextBoxFor() binds to the value of model property. If the value of the property is DateTime.Now, then that is what will be displayed.
Yes it works. Setting the value as model value @Html.TextBoxFor(m => m.ToDate, new { @class = "datepicker", placeholder = "Enter From date here...", Value = Model.ToDate }) sets back the date field with the changed model value

Your Answer

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