Click here to Skip to main content
15,887,464 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I am working on a ASP.NET MVC 4 application and I have difficulty with Binding date to jQuery DatePicker in Edit view of particular controller that having one field say "PurchaseDate" with type DateTime. I have set all date format with Data Annotation. But still it is not working in Edit mode as expected. I am using bootstrap-datepicker.

Please reply if any have proper solution for same. Please refer below code:

[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-mm-yyyy}")]
public DateTime PurchaseDate { get; set; }


And Edit view as below:

@Html.TextBoxFor(model => model.PurchaseDate, new { @class = "form-control date-picker", value = Model.PurchaseDate.ToString("dd-mm-yyyy") })

And I am applying bootstrap-datepicker as below:


$('.date-picker').datepicker({ rtl: App.isRTL(), autoclose: true });
Posted
Updated 2-Jan-14 17:35pm
v2
Comments
♥…ЯҠ…♥ 2-Jan-14 8:41am    
Update your question with what you have tried so far

1 solution

$(document).ready(function () {

$(function () {
$("#Date").datepicker();
});
});

@Html.TextBoxFor(m => m.Date, new { @class = "Date" })
@Html.ValidationMessageFor(m => m.Date)

Controller:

[HttpGet]
public ActionResult Edit(string _EditId)
{
if(!String.IsNullOrEmpty(_EditId))
{
int ID = Convert.ToInt32(_EditId);
var objtable = ( select Date from tablename where tbl_Id = ID);
return View(objtable);
}
}

[HttpPost]
public ActionResult Edit(model.data obj)
{
// write your Code Here.....
}

get the date in Edit mode for this Code i think its helpfull for you...
 
Share this answer
 
Comments
Bhavesh Jogani IT 2-Jan-14 23:40pm    
Hello Madhavi thanks for your reply. I have already tried like this, but the date format issue remains. Please refer updated question for better idea and give me your suggestions...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900