Home > other >  Datetime from API , Create/post to API
Datetime from API , Create/post to API

Time:12-19

I got two different projects, one API and one for the web(front). I Can create a course through my API but when i try to post from my front-end i got this error,

FormatException: Input string was not in a correct format.

And it complains on this one,

<span asp-validation-for="CourseTitle" ></span>
        </div>
          <div >
            <label asp-for="CourseStart"  style="font-size:20px;"></label>
        </div>
        <div >
            <input asp-for="CourseStart"  />
            <span asp-validation-for="CourseStart" ></span>
        </div>
            <div >
            <label asp-for="CourseEnd"  style="font-size:20px;"></label>
        </div>
        <div >

Somehow my CourseStart doesnt work, and my class looks like this,

 [Key]
        public int CourseId { get; set; }
        [Required]
        public string CourseTitle { get; set; }

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

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

The problem i think i got is that when i save a course through the API i got Date like this,

 "courseStart": "2022-12-15T00:00:00"

So i think its expecting T00:00:00 somehow? What do u think? I cant find anything on google....

CodePudding user response:

I made it work, what i did was, changed my class to this,

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

after that, inside my html file, i put in this,

<div >
     <label asp-for="CourseStart"  style="font-size:20px;"></label>
            </div>
            <div >
                <input asp-for="CourseStart" type="date"   />
                <span asp-validation-for="CourseStart" ></span>
</div>

problem was that, through html helper, it saved it in this format, yyyy-MM-dd, so i had to change it. U can probably customize it but its to complicated for me :)

  • Related