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?
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; }
}
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)
);
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?.
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?
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.
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?
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.
[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
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
None
0 Points
8 Posts
working with DateType
May 19, 2020 10:05 AM|binaarycode|LINK
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
Model
Controller
Database
All-Star
53711 Points
24042 Posts
Re: working with DateType
May 19, 2020 03:54 PM|mgebhard|LINK
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?.
None
0 Points
8 Posts
Re: working with DateType
May 20, 2020 08:18 AM|binaarycode|LINK
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?
All-Star
53711 Points
24042 Posts
Re: working with DateType
May 20, 2020 09:41 AM|mgebhard|LINK
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
None
0 Points
8 Posts
Re: working with DateType
May 20, 2020 11:14 AM|binaarycode|LINK
Just realised I was ahead of myself with the html page
All-Star
53711 Points
24042 Posts
Re: working with DateType
May 20, 2020 11:31 AM|mgebhard|LINK
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?
None
0 Points
8 Posts
Re: working with DateType
May 20, 2020 12:05 PM|binaarycode|LINK
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.
Contributor
4050 Points
1575 Posts
Re: working with DateType
May 21, 2020 09:33 AM|yij sun|LINK
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:
Controller:
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:
Best regards,
Yijing Sun