1

I have a standard asp.net mvc 4 view with a form that has some editable fields, including a DataTime selection. When the form is posted back to the controller, the DateTime always shows up as the minimum value for DateTime 1/1/0001, even though a valid date is successfully sent to the client or selected on the client.

I'm guessing the DefaultModelBinding is messing this up, but I'm not sure why or how. In my web.config I have the following for globalization:

<globalization enableClientBasedCulture="true" uiCulture="auto" culture="auto"/>

Here is my ActionResult in the controller that receives the post:

[HttpPost]
public virtual ActionResult Edit(KBUser kbuser)
{
    if (this.ModelState.IsValid)
    {
        //save the user
        m_userRepo.UpdateUser(kbuser);


        return RedirectToAction(MVC.Users.Index());

    }
    else
    {
        return View(kbuser);
    }

}

Here is the relevant part of the View:

@model KBMaxLive.Domain.KBUser

@using (Html.BeginForm("Edit", "User", new { idUser = Request["idUser"] }, FormMethod.Post, new { id = "form" }))
    { 
        <fieldset>
            <legend>@Engine.Resources.KBMaxLive.Account</legend>

             <div class="field-column-container">
                <div class="field-column">
                    <div class="field-container">
                        @Html.LabelFor(u => u.CreatedDate)
                        @Html.TextBoxFor(u => u.CreatedDate, new { @class = "k-textbox", @readonly = "readonly" }) 
                        @Html.ValidationMessageFor(u => u.CreatedDate)
                    </div>
                </div>
            </div>
        </fieldset>
    }

All properties of the model come in fine except for the datetime.
Any ideas on what's going wrong or how to debug this?

kbmax
  • 614
  • 1
  • 6
  • 15
  • DateTime.MinValue is the default value for a DateTime. I suspect you have a mismatch between your property name and the parameter name or the value isn't parseable as a DateTime. – tvanfosson Oct 14 '12 at 13:41
  • Edited to show the relevant part of the Edit view. Although I'm not sure the view is the problem, as even if I don't have a field that shows the DateTime, it still comes back into the post as the DateTime.MinValue – kbmax Oct 14 '12 at 13:48
  • Can you use FireBug or Chrome inspector to determine if the value is actually being sent back with the POST? Also, can you show us your model? Note: I typically wouldn't set a created date on the model using values from the POST anyway (the user could easily use FireBug to simply change the value overriding your readonly setting); I'd set it in the back end. – tvanfosson Oct 14 '12 at 14:06
  • tvanfosson, see my comment on the answer below. The date seems ok in the POST in Firebug, and appears to be in the right format. It just doesn't make it into the controller – kbmax Oct 15 '12 at 23:51

1 Answers1

0

The default model binder uses the current culture when parsing datetimes for POST requests. From the snippet of your web.config you have shown it looks like you are using auto. This means that the current culture will be determined by the client browser preferences. For example in Google Chrome you could configure a list of languages and their order of preference. The browser will then send the Accept-Language header with each request.

For example:

Accept-Language:en-US,en;q=0.8

ASP.NET MVC will then use this language to set the culture for the request. This means that the model binder will expect the datetime to be entered in the format defined for this language. For en-US that will be MM/dd/yyyy. If you have configured your browser to use a different preferred language then the user needs to type the datetime according to this preference.

On the other hand if you want all dates to be in a specific format no matter the client browser configuration you could write a custom model binder. For example I have shown how this could be done at this answer which uses the DisplayFormat attribute on the corresponding view model property.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • In Firebug, the Accept-Language header comes in as 'en-us,en;q=0.5'. Also, the CreatedDate comes through in a format like this: '8/23/2011 9:59:10 PM'. The format looks fine to me for the language setting, and even confirmed this with a test DateTime.Parse("8/23/2011 9:59:10 PM") which succeeds. Still though, coming through as the MinValue – kbmax Oct 15 '12 at 23:51