Tech Off Thread

3 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Recommended way to process DateTime in ASP.net MVC4

Back to Forum: Tech Off
  • User profile image
    c9pedro

    Hi guys, I'm new to this framework and want to know how to solve the following problem by the recommended way using the framework:

    I am using a model, that has a  DateTime attribute.

    Everthing runs fine when I create an entity using EF6. But when it comes to editing the value, the input field displays the value from the database with its time. Also the datepicker doesnt work when I delete the value. Chaning the value has no effect, except an validation error.

    Here are the things I already tried:

    - Model usings annotations for data format:

    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yyyy}")]
    public DateTime Mydate { get; set; }

    - View reading the model:

    <div class="form-group">
                @Html.LabelFor(model => model.Mydate, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Mydate.Date, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Mydate, "", new { @class = "text-danger" })
                </div>
            </div>

     

    Somebody gave me the advice, that it is bad code to set the DateFormat in the model and I should better use globalisation at webconfig. So I did the following changes to my project. Creating runs fine as before, but editing stays still the same:

    - added globalisation tag to <system.web> @webconfig

    <globalization culture="de-DE" uiCulture="de-DE"/>
    <globalization culture="de-DE" uiCulture="de-DE"/>


    - changed my model by deleting dataformat annotations:

    [DataType(DataType.Date)]
    public DateTime Mydate { get; set; }


    Thanks in advance

  • User profile image
    figuerres

    I recommend that you do some research on DateTime and DateTimeOffset and .net and sql server.

    datetime has a number of issues that can come up that DateTime Offset handles better esp differences between local and UTC time base.

    adjusting date time values, handling local time zones and such are sometimes not easy and can have tricky issues to deal with.

    if you can always use UTC date and time inside your application and then adjust to local as needed.

    I have seen dates get thrown off by several hours due to getting a date time and then sending it to another location with a different timezone.

    that happens with datetime as it will assume that values are in local time when it deserializes the data.

     

     

  • User profile image
    spivonious

    @c9pedro: It sounds like your problems are the client datepicker ( is this using a jQuery UI datepicker? Built into the browser?

    You should also just use model.Mydate in the call to EditorFor(). If your value isn't getting sent to the server, we'll need to see the whole view and the controller's post method.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.