Problem DateTime parameters for PopupActionLink and PopupFormActionLink

Mar 9, 2011 at 8:54 PM

Problem:
I have a controller that accepts DateTime as one of its parameters. When the Popup(Form)ActionLink is generated all parameters are serialized as JSON. By default DateTime will be output in MS special format "\/Date(xxxxxx)\/". The default model binder does not handle this format when sent back in the querystring.

Solution:
The problem could be solved by using a custom converter that output a more friendly format for DateTime in links, i.e. "YYYY-DD-MM hh:mm:ss.fff" or "new Date(xxxxxx)".

Workaround:
Currently i work around the problem by specifying the parameter as DateTime? and then specifically convert the (by then invalid) MS JSON-date:

        public static DateTime GetDateTimeParameter(this Controller controller, string name)
        {
            ValueProviderResult value = controller.ValueProvider.GetValue(name);
            if (value != null && value.RawValue != null && !String.IsNullOrWhiteSpace(value.AttemptedValue))
            {
                return (new JavaScriptSerializer()).Deserialize<DateTime>("\"" + value.AttemptedValue.Replace("/", "\\/") + "\"").ToLocalTime();
            }
            return new DateTime();
        }

Coordinator
Apr 5, 2011 at 1:21 PM

there is also another way of creating actionlinks

the Html.PopupFormActionLink(action:"myaction", controller:"home")

will generate

<a href='javascript:pfmyactionhome(p1,p2,p3)'/> (you can look at the html source of your pages)

so you can write this <a> yourself by specifying the parameters the way you want to (I do understand this is not a strongly typed, but it's gonna work)