locked
working with DateType RRS feed

  • Question

  • User-1283341821 posted

    Currently trying to store a date along with a start time and an end time within a database. but I'm currently running into an issue with the date is being stored as "0001/01/01" and the time "00:00:000000000" which I believe from google searches it's the default values of each.

    Should I be looking at pulling all the form fields into variables and then storing them as strings? or is there a way to actually store times and dates? 

     View

       <label>Date of Event</label>
       <div class="form-group">
            <input id="EventDate" type="date">
        </div>
    
        <label>Start Time</label>
        <div class="form-group">
             <input id="StartTime" type="time">
         </div>
    
         <label>End Time</label>
         <div class="form-group">
             <input id="EndTime" type="time">
         </div>

    Model

        public class Events
        {
            public int Id { get; set; }
            public int UserId { get; set; }
            public DateTime EventDate { get; set; }
            public DateTime StartTime { get; set; }
            public DateTime EndTime { get; set; }
        }
    

    Controller

    // create 
    [HttpPost]
    public async Task<IActionResult> CreateEvent([Bind("UserId,EventDate,StartTime,EndTime")] Events Events)
            {
    
                if (ModelState.IsValid)
                {
                    _context.Add(Events);
                    await _context.SaveChangesAsync();
                    return RedirectToAction(nameof(EventCreated));
                }
                return View(Events);
            }

    Database

    CREATE TABLE [dbo].[Events] (
        [Id]        INT          IDENTITY (1, 1) NOT NULL,
        [UserID]    INT          NULL,
        [EventDate] DATE NULL,
        [StartTime] TIME NULL,
        [EndTime]   TIME NULL,
        PRIMARY KEY CLUSTERED ([Id] ASC)
    );

    Tuesday, May 19, 2020 10:05 AM

All replies

  • User475983607 posted

    You are confusing a DateTime type with a human readable date format.  The HTML inputs do not have name attributes.  There's nothing for the browser to submit.  

    The Event class is populated with an empty DateTime type which defaults to 0.  If you expect the date to have a value then you have to write validation code.    If you expect null values then define the date as a nullable type DateTime?.

    Tuesday, May 19, 2020 3:54 PM
  • User-1283341821 posted

    Thanks for the reply! 

    Didn't even notice the asp-for=" " was missing, serves me right for trying to write the code myself. It's now being stored into the database, although by using DateTime I'm also storing "00:00:00" along with the date and the seconds are being stored in time. Is this going to cause issues if I try and display or pull the date into another page?  

    Wednesday, May 20, 2020 8:18 AM
  • User475983607 posted

    It's now being stored into the database, although by using DateTime I'm also storing "00:00:00" along with the date and the seconds are being stored in time. Is this going to cause issues if I try and display or pull the date into another page?

    It's not clear why or how this is a problem in your application but you have total control over the DateTime presentation.

    References

    Standard date and time format strings

    Custom date and time format strings

    Wednesday, May 20, 2020 9:41 AM
  • User-1283341821 posted

    Just realised I was ahead of myself with the html page 

    @model IEnumerable<Aspnetcorewebsite.Models.Events>
    @{
        ViewData["Title"] = "View";
    }
    
    
    <div class="container">
        <div class="pricing-header px-3 py-3 pt-md-5 pb-md-4 mx-auto text-center">
            <h1> Welcome @User.Identity.Name</h1>
            <h2 class="display-4">User Table</h2>
            <p class="lead">Below is a list of users who currently have user accounts in the database</p>
        </div>
    
    
        <table class="table table-striped table-bordered usertable">
            <thead>
                <tr>
                    <th>Event Date</th>
                    <th>Start Time</th>
                    <th>End Time </th>
                    <th>Edit         </th>
                    <th>Remove     </th>
                </tr>
            </thead>
            <tbody>
    
                @if (Model != null)
                {
                    @foreach (var item in Model.Where(item => item != null))
                    {
                        <tr>
                            <td>
                                @Html.DisplayFor(modelItem => item.EventDate)
                            </td>     
                            <td>
                                @Html.DisplayFor(modelItem => item.StartTime)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.EndTime)
                            </td>
                            <td>
                                @Html.ActionLink("", "", new { id = item.Id })
    
                            </td>
                            <td>
                                @Html.ActionLink("", "", new { id = item.Id })
                            </td>
    
    
                        </tr>
                    }
                }
            </tbody>
        </table>
        <div class="clear">&nbsp;</div>
        <!-- 		<a class="btn btn-outline-danger" href="">Save</a> -->
    
    </div>
    
    
    

    Wednesday, May 20, 2020 11:14 AM
  • User475983607 posted

    You've posted a view with tag helpers which indicates you are building an ASP.NET Core application.  The error is related to an HTML Helper which is generally found in ASP.NET MVC but can be in a Core app.   

    Can you clarify what you are doing?  Why is there a mix between HTML helpers and tag helpers?

    Wednesday, May 20, 2020 11:31 AM
  • User-1283341821 posted

    At the minute i'm trying to allow an account to create an event with the Date, Start Time and End Time which was the original problem of storing the dates within the database.

    Now, I'm trying to get a page to pull up all the events by the logged in user ID into a table.From here I want them to be able to edit and delete these events. I realised I was ahead of myself with the pages (updated previous post) but I imagine I'm still running into the same issue. 

    I've managed to do it with a user table and an edit venues page using the same way. 

    Wednesday, May 20, 2020 12:05 PM
  • User1535942433 posted

    Hi binaarycode,

    Accroding to your description,I create a demo and it works fine,Do you have used Date in the database?Do you have changed the view codes?

    Could you post your current full codes to us?It will help us to solve your problem.

    More details,you could refer to below codes:

    View:

    <label>Date of Event</label>
       <div class="form-group">
            <input asp-for="EventDate" type="date">
        </div>
    
        <label>Start Time</label>
        <div class="form-group">
             <input asp-for="StartTime" type="time">
         </div>
    
         <label>End Time</label>
         <div class="form-group">
             <input asp-for="EndTime" type="time">
         </div>

    Controller:

    [HttpPost]
    public async Task<IActionResult> CreateEvent([Bind("UserId,EventDate,StartTime,EndTime")] Events Events)
            {
    
                if (ModelState.IsValid)
                {
                    _context.Add(Events);
                    await _context.SaveChangesAsync();
                    return RedirectToAction(nameof(EventCreated));
                }
                return View(Events);
            }

    Model:

      public class Events
        {
            public int Id { get; set; }
            public int UserId { get; set; }
            public DateTime EventDate { get; set; }
            public DateTime StartTime { get; set; }
            public DateTime EndTime { get; set; }
        }

    Database:

    CREATE TABLE [dbo].[Events] (
        [Id]        INT          IDENTITY (1, 1) NOT NULL,
        [UserID]    INT          NULL,
        [EventDate] DATE NULL,
        [StartTime] TIME NULL,
        [EndTime]   TIME NULL,
        PRIMARY KEY CLUSTERED ([Id] ASC)
    );

    Best regards,

    Yijing Sun

    Thursday, May 21, 2020 9:33 AM