Reputation: 6027
I am passing a model to a view partial containing a form that is being loaded asynchronously. Everything seems to be coming across as expected EXCEPT for a DateTime proprty, what would cause this to happen?
Javascript function
function loadUpdateEventForm(eventID) {
getEventDetails(eventID, function(dnnEvent) {
if(dnnEvent != null) {
$("#updateEventForm").load(urlEditEventForm, dnnEvent, function () {
$("form#updateEventForm").submit(function (event) { submitNewEvent(event); });
});
dialog = $("#updateEventForm").dialog({ modal: true, width: '80%', position: { my: 'top', at: 'top+150' } });
console.log(dnnEvent);
return;
}
});
}
Output from console.log(dnnEvent)
Object {EventID: 2524, EventName: "sample", EventDescription: "sample", EventTimeBegin: "/Date(1418709600000)/", UserID: 1}
C# Action method serving partial view
public ActionResult _EditForm(DNNEventUpdateModel dnnEvent)
{
return View(dnnEvent);
}
DNNEventUpdateModel
public class DNNEventUpdateModel
{
[Required]
public int EventID { get; set; }
[Required]
public string EventName { get; set; }
[Required]
[DataType(DataType.MultilineText)]
public string EventDescription { get; set; }
[Required]
public DateTime EventTimeBegin { get; set; }
public int UserID { get; set; }
public string EventTimeBeginForDisplay
{
get
{
return this.EventTimeBegin.ToShortDateString();
}
set
{
this.EventTimeBegin = Convert.ToDateTime(value);
}
}
}
Just realized I forgot to post how this is coming across in the controller. Here is what I mean about the date:
Upvotes: 0
Views: 114
Reputation:
The date format EventTimeBegin: "/Date(1418709600000)/"
is a function of default JsonScriptSerializer
and is discussed in more detail in the answers to this question.
You can parse the value to a string using
new Date(parseInt(response.TimeBegin.replace("/Date(", "").replace(")/",""), 10))
Other options including formatting the date in the controller method, for example
var data = new
{
....
TimeBegin = YourDate.ToString("o"),
....
};
return Json(data, JsonRequestBehavior.AllowGet);
and in the script
new Date(response.TimeBegin);
or using another json serializer such as Json.NET which serializes dates in ISO format.
This blog also discusses some client side and server side solutions.
Upvotes: 1
Reputation: 38367
It looks like you're doing a form post.
$("form#updateEventForm").submit(function (event) { submitNewEvent(event); });
So the model binder on the MVC side does NOT use a JSON parser. I am guessing submitNewEvent stuffs the values from the event into form fields. since the JSON model binder is not involved, the form field coantaining a JSON formatted date, is not handled properly.
MVC will handle JSON posts if you used something like $.post
and posted the JSON string verbatim instead of stuffing a form. This way the JSON parser is used during model binding, and the JSON formatted date will be properly handled.
Otherwise if you're committed to using your form post, you need to populate the form with a properly formatted date before posting it. Which would be 'YYYY-MM-DD' or in other words <input value='2014-01-30'>
. You can't blindly take a JSON formatted date string and plug it into a form field.
Of course I'm making assumptions based on javascript function calls you have that we don't have the code for.
Upvotes: 0
Reputation:
Maybe there is better solution but I send javascript date as string to server, try that.
var date = new Date();
var day = date.getDay();
var month = date.getMonth();
var year = date.getFullYear();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
var time = day + "/" + month + "/" + year + " " + hour + ':' + minute + ':' + second;
Then, parse the string in server - side.
DateTime.ParseExact(dateFromClient, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
EDIT
I used once the function before sending date.
function EditDate(oldDate) {
var date = oldDate.split(".");
return date[2] + "." + date[1] + "." + date[0];
}
Upvotes: 1