0

I have a model contains a datetime field. When i submit form all fields are bound viewmodel but datetime field is set 1.01.0001 00:00:00. I updated format but it didn't change. How can i solve it?

<form class="content" method="post" asp-controller="User" asp-action="SaveUser">                
    <div class="form-group date-time-picker label-floating">
        <label class="control-label">Birthday</label>
        <input type="text" asp-for="Birthday" name="datetimepicker" />
        <span class="input-group-addon">
            <svg class="olymp-month-calendar-icon icon"><use xlink:href="#olymp-month-calendar-icon"></use></svg>
        </span>
    </div>
    <input type="submit" class="btn btn-purple btn-lg full-width" value="Save" />
    </div>
    </div>
</form>

Model:

 public class SaveUserVm
 {        
      [Required(ErrorMessage = "Birthday Required")]
      [DataType(DataType.Date)]
      public DateTime Birthday{ get; set; }
 }

Save action

 [HttpPost]
 public async Task<SaveUserVM> Save(SaveUserVM user)
 {
     var result= await memberApiService.SaveUser(user);

     return result;
 }
Trinity
  • 486
  • 8
  • 27
  • Do you want a current date? `public DateTime Bla {get; set;} = DateTime.Now;` – Sinatr Jul 15 '20 at 12:31
  • Dates have no format, they are binary values. Formats apply only when a string is parsed to return a date. *What* format was used? If it was anything other than the ISO8601 format (YYYY-MM-DD) the string will be parsed using the end user's or the server's locale – Panagiotis Kanavos Jul 15 '20 at 12:32
  • That's the bitwise zero default value of `DateTime`. If you want it to be `null` when unset make it nullable:`public DateTime? MyDateTimeProp { get; set; }` – György Kőszeg Jul 15 '20 at 12:33
  • Change ` ` to [](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date) to prevent users from entering invalid values or formats. The browser will display a date picker and the value will be in the ISO8601 format – Panagiotis Kanavos Jul 15 '20 at 12:35
  • If you want to throw an error when the DateTime doesn't have a value entered, check for this default value and throw an error manually. – Mr Lister Jul 15 '20 at 12:35
  • There are obviously some missing steps. The HTML input is bound to "Birthday" but the property is named "DogumTarihi". Besides that, you can use the `DisplayFormat` attribute to control the formatting of the date. See https://stackoverflow.com/questions/5252979/assign-format-of-datetime-with-data-annotations – Heretic Monkey Jul 15 '20 at 12:40
  • The problem reason is not that but i fixed it. – Trinity Jul 15 '20 at 12:42
  • I just want to send a birthday like 25/04/1988. That's all. – Trinity Jul 15 '20 at 12:50
  • 1
    Please show the **exact** payload being submitted to the server (from Chrome Developer Tools -> Network tab). – mjwills Jul 15 '20 at 13:39

0 Answers0