Reputation: 6290
I've been trying to post a form to my controller:
Id=0&ReportDate=2010-08-09T00%3A00%3A00&SampleText=Save
That's the XHR post that is sent, my controller picks up all the properties except ReportDate, instead setting it to the .NET epoch DateTime. Any ideas?
Edit: If I set another variable, ReportDateString
, send the string to the controller and do a DateTime.Parse()
, it works fine. However, I'd really like to be able to bind the DateTime directly as this feels hacky.
Edit 2: Hereis my controller code:
public void CreateTest(MyObject myObject) {
myObjectRepository.Update(rootObject);
}
And my object:
public class MyObject {
public int Id { get; set; }
public string SampleText{ get; set; }
public DateTime ReportDate { get; set; }
}
If I set a debug, I can see that the model binder successfully binds all the properties on my post except the DateTime which it sets to the epoch date.
Edit 3: Form:
<form id="testform" method="post">
<input type="hidden" name="Id" value="0" />
<input type="hidden" name="ReportDate" value="2010-08-09T00-00-00" />
<input type="text" name="SampleText" value="Test"/>
<button id="saveButton">Save</button>
</form>
Javascript:
$('#saveButton')live('click', function(e) {
$.post('CreateTest', $('#testform').serialize())
});
Upvotes: 2
Views: 1812
Reputation: 124
It is not that tricky, you need to add cultural settings to your webconfig. This should solve your problem. In my case it is TR, you can try adding yours
<system.web>
<customErrors mode="Off" />
**<globalization enableClientBasedCulture="true" culture="tr-TR" uiCulture="tr-TR" />**
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
Upvotes: 0
Reputation: 9676
This works just fine for me:
public void Test2(DateTime ReportDate, string SampleText, int Id)
with the url:
/Home/Test2?Id=0&ReportDate=2010-08-09T00:00:00&SampleText=Save
ReportDate is then {09.08.2010 00:00:00} when I break in my code...
Edit - Additional Code:
<% using (Html.BeginForm())
{ %>
<%= Html.Hidden("ReportDate", "2010-08-09T00:00:00") %>
<%= Html.TextBox("SampleText", "Save") %>
<%= Html.TextBox("Id", "1") %>
<input type="submit" />
<%} %>
I also tried the following with the same result:
<form id="testForm">
<%= Html.Hidden("ReportDate", "2010-08-09T00:00:00") %>
<%= Html.TextBox("SampleText", "Save") %>
<%= Html.TextBox("Id", "1") %>
<a href="#" id="submitform">Submit!</a>
</form>
<script type="text/javascript">
$("#submitform").click(function () {
$.post("/Home/Test2", $("#testForm").serialize());
});
</script>
And this:
[HttpPost]
public ActionResult Test2(MyObject myObject) {
return View();
}
Where MyObject is a copy/paste from your original question..
Upvotes: 1