0

I am posting DateTime value as a String Via AJAX POST to MVC Action for saving this Value.

But at the Time of Saving the Value in MVC Action, I get the error message in My AJAX response as:

The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value

My AJAX POST is as,

String dateTime="2013-07-25 11:59:22 PM";
$.ajax({
    type:'POST',
    url:'SaveSchedule',
    data:{start:dateTime},
    success:function(data){ }
});

and MVC Action as,

[HttpPost]
public ActionResult SaveSchedule(DateTime start)
{
    var schedule = new Schedule { StartTime = start};
    db.Schedules.Add(schedule);
    db.SaveChanges();
    var temp = 0;
    return Json(temp);
}
Falko
  • 17,076
  • 13
  • 60
  • 105
user2624970
  • 23
  • 1
  • 1
  • 4

1 Answers1

2

While using ajax function, you should be careful as they are the client side function, hence you should decalre string type parameter in your controller, and within the action convert them into the required data type by using the tryparse methods of C#. after getting date in string parameter you will convert the date in string to datetime datatype by using datetime.tryparse method, then you will never get such errors while using ajax.

Veenu Singh
  • 51
  • 1
  • 1
  • 5
  • I have also Tried with that "TryParse", then also I am getting Error Like, String was not recognized as a valid DateTime. on Line DateTime startTime = DateTime.ParseExact(s.StartTime, "yyyyMMdd HH:mm:ss.fff", null); – user2624970 Jul 27 '13 at 06:54