views:

98

answers:

1

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())
});
A: 

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..

Yngve B. Nilsen
How are you preventing the colons from changing to %3? That might be the difference. I'm serializing a form and the ReportDate is a hidden input field.
chum of chance
Updated the post with the form-code I've used... I don't do anything else than what you see in the post, so it's weird that it doesn't work for you....
Yngve B. Nilsen
it doesn't matter that colons change to %3 in the post, because that's the way HTTP encoding works... for instance a space becomes %20 in an URL... So that's not your problem... I would guess your problem might be in the actual form html-code..
Yngve B. Nilsen
Ah yes, I forgot that it decodes prior to the model binding, so the HTTP encoding wouldn't be the issue.
chum of chance
I'll take you up on your Occam's razor bet, I've posted my form and javascript, pretty identical to yours. I can confirm it does post the string at the top of my question (using Firebug and Fiddler, both produce that). All the other values get bound to the object.
chum of chance
As Occam's razor says - The simplest solution is usually the right one: in <input type="hidden" name="ReportDate" value="2010-08-09T00-00-00" /> try to replace the hyphens in the timedesignation with colons ;)
Yngve B. Nilsen