简体   繁体   中英

Form don't accept DateTime format

Need some help here. I have the standard edit form the default visual studio application, and when I use the form to edit some DB row, in the datetime it shows me a date like "01-01-0001 00:00:00", and to save the edit i have to change the date to "01/01/0001 00:00:00". Its boring, and as it is the date bringed by mvc itself, it should work if I don't touch it right? My code is the follow:

Model

    public class Result
    {
        [Display(Name = "ID")]
        public Int32 Id { get; set; }
        [Display(Name = "Data/Hora")]
        public DateTime Datahora { get; set; }
    }

View (where the form is)

@model ApdlModel.Entities.Resultado

@{
    ViewBag.Title = "Edit";
}

<h2></h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Editar Resultado</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.Id)

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

<div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Guardar" class="btn btn-default" />
            </div>
        </div>
    </div>
}

Basically, when the mvc gets the value from the BD, it brings the "01/01/0001 00:00:00" format, but when I try to save in that format it dont allow me. Why? What I am doing wrong?

Maybe the problem is because your date is not nullable? If it is not nullable you should consider setting default value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM