1

Here's my view:

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <div class="editor-label">
            @Html.LabelFor(model => model.Title)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Title)
            @Html.ValidationMessageFor(model => model.Title)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Description)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Description)
            @Html.ValidationMessageFor(model => model.Description)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ExpireDate)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ExpireDate)
            @Html.ValidationMessageFor(model => model.ExpireDate)
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

(basically the template view)

Here's my C# that it posts to:

[HttpPost]
public ActionResult Create(bulletin newBulletin)
{
    try
    {
        var db = Util.GetDb();
        var toAdd = new bulletin
            {
                title = newBulletin.title,
                description = newBulletin.description,
                expire_date = newBulletin.expire_date,
                timestamp = DateTime.Now,
                user_id = 1
            };
        db.bulletins.InsertOnSubmit(toAdd);
        db.SubmitChanges();
        return RedirectToAction("Index");
    }
    catch(Exception ex)
    {
        return View();
    }
}

Title and Description are populated, but no matter what date I put in the expire_date text box, it comes through as:

{1/01/0001 12:00:00 AM}

Any clues?

tereško
  • 58,060
  • 25
  • 98
  • 150
Ben Power
  • 1,786
  • 5
  • 27
  • 35
  • try like this `@Html.LabelFor(model => model.ExpireDate.ToString("dd MMM yyyy"))` – Karthik Chintala Mar 08 '13 at 05:39
  • Then I get the exception: "Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions." – Ben Power Mar 08 '13 at 05:51
  • OK, resolved this. Turned out I was using the entity class instead of the model class, and the mvc framework reconciled all the similarly named properties except the date, which had an underscore in it. A good quirk to be aware of. – Ben Power Mar 10 '13 at 23:00

1 Answers1

2

When the model binder tries to parse the request value to a DateTime field it will use the current thread culture. So for example if in the <globalization> element you have set some specific culture you should make sure that the date string you have entered in the textbox is in the correct format.

If on the other hand you do not have a globalization element in your web.config or set the culture to auto (default value) then the current culture will be determined by the client. The client web browser sends an Accept-Language request header containing the value to be used. In this case you will need to use the same date format as the one configured in your browser.

And if you want to have all your dates in a specific format, no matter what browser settings are used, you could write a custom model binder that will use the DisplayFormat attribute on your view model when biding the value. I have illustrated an example of such a model binder in this post.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928