I realize this question has already been asked plenty of times. However none of the solutions worked for me.
The problem:
I have an object:
public class SomeObject
{
[Remote("MyAction", "MyController")]
public DateTime MyDate { get; set; }
}
And a model:
public class SomeModel
{
public SomeObject MyObject { get; set; }
}
and my action:
public JsonResult MyAction(DateTime MyDate)
{
//do some validation here
}
When the validation kicks in, because its a complex object it sends a URL like this:
http://localhost/MyController/MyAction?MyObject.MyDate=xxxxxxxx
Obviously this means that my action wont pick up the date because the parameter name does not match the querystring name.
I've searched far and wide and saw lots of suggestions - all of which I've tried but none have worked. The following was what I tried:
Using the bind attribute
public JsonResult MyAction([Bind(Prefix = "MyObject")]DateTime MyDate)
and
public JsonResult MyAction([Bind(Prefix = "MyObject.MyDate")]DateTime MyDate)
the date just comes back as null
Using the model or the object as the parameter type
public JsonResult MyAction(SomeObject myObject)
and
public JsonResult MyAction(SomeModel myModel)
MyDate
always ends up as MinValue, but the parameters (SomeModel or SomeObject) are not null
I'm not quite sure what I'm doing wrong. Can anyone point me in the right direction? Are there any incantations I have to chant prior to running, or perhaps does it have to be sunny outside? This seems to be far more work for a common scenario than expected.
So it turns out that the problem is not what I thought it was. The problems lie with the jQuery UI Datepicker, which is sending the PREVIOUS value through to the action and with my culture settings. The datepicker is formatted with dd/MM/yy
but MVC tries to interpret that as a US style MM/dd/yy
even though DateTime.Parse
parses it correctly. Thats the main reason why the date comes back as null. I will be writing questions around those two issues.
As for the solution - using the following does actually work:
public JsonResult MyAction(SomeObject myObject)