Setting a minimum date input value - c#

My Razor Pages application has a modal form that allows the user to enter the date. And I bind this to a property in my model.
[BindProperty]
public DateTime? TripDate { get; set; }
Two questions:
Is it possible to set a minimum valid value?
Is it possible to change this minimum valid value from JavaScript/jQuery at run time?
I want to set a different minimum value depending on which date is being entered. But I could also set the minimum value from the server side if that's easier.

Related

Issue -RadGridView - GridViewDateTimeColumn editor does not allow max value 09/09/9999

To Reproduce:
1. Add a GridViewDateTimeColumn to a RadGridView.
2. Try to set RadDateTimeEditor.MaxValue in CellEditorInitialized event to 31/12/9999.
3. As a result, ArgumentOutOfRangeException("MaxDate cannot be higher
than the max date") is thrown
For your case with GridDateTimeColumn you should use the SharedCalendar property of the RadDatePicker. The SharedCalendar property holds a shared instance of the calendar in order to speed up performance when we use the calendar on many places. This is the case with the grid having multiple items each having a calendar, that is why the GridDateTimeColumn makes use of the SharedCalendar property instead of the standard one.
Also here the relevant property for the maximum date is called RangeMaxDate
RadDatePicker rdp = cell.Controls[0] as RadDatePicker;
rdp.SharedCalendar.RangeMaxDate = setMaxDate;

EntityFramework Data Annotations required null integer

I have a small question about EntityFramework Data Annotations (Code First).
I want to make integer / decimal required.
[Required]
public int? Nummer { get; set; }
But I have a small problem with this. The integer can't be null in my WPF application. Look at the screenshot bellow:
Because of the required the property wont change to null when the textbox is empty.
It's clearly visible the Selected row still has a number, 3 while it should be empty...
I don't have this problems with 'required' strings.
Why is this a problem? Because now the Opslaan (save) button doesn't get disabled when the number is 'empty'.
I can fix this by doing my data validation again manually.
With a switch and the IDataErrorInfo implementation.
Does someone know if I can solve this with the aid of the Data Annotations?
You could update your binding of your text box like this:
<TextBox Text="{Binding Nummer, TargetNullValue=''}"/>
With this binding the Nummer property gets set to null when the value of the text box is an empty string. If you don't specify TargetNullValue, the empty string cannot be converted to an int? and therefore you get a conversion error.
Just a question to get you right: When you want your Nummer to be required, why do you use int? as the data type instead of int?

DateTime gives me "01/01/0001 12:00:00 AM" instead of actual date

I'm using MVC 4 and I have a form field that accepts two dates. One for checkin and the other for checkout. The class is very basic:
public class date {
public DateTime Checkin { get; set; }
public DateTime Checkout { get; set; }
}
The format I'm using is mm/dd/yyyy where the user inputs the checkin date using a javascript calendar and adjusts the checkout date with the jquery mobile slider. However, I seem to always get
"01/01/0001 12:00:00 AM" instead of the actual date. I gave the solution to my co-workers and they do not have the problem even though its the exact same code.
Is there a setting that I can change or is my visual studio messed up?
Do you have the code where those properties are being assigned? That's the default value for DateTime, so whatever is supposed to be assigning it is probably not executing at all. Is date a model? If you're relying on the mvc model binder, make sure your controller action accepts a parameter of type date. (and maybe think about improving the name also)
The class you have shown initializes Checkin and Checkout to DateTime.MinValue, which is 12am on 01/01/0001.
You do not show any code that changes that value from the initialized value.
Presumably the binding from the UI to the model is incorrect. You would have to post more code to diagnose what is wrong exactly. Suggest you post the action in the controller where you accept new values from the UI.
For guidance on what can go wrong using a DateTime as a parameter to a controller action, see this similar post:
How do I pass a datetime value as a URI parameter in asp.net mvc?
Your binding on the client side is not working, please take a look at the names of the input that you are using for the dates, for example, check below, you can have the id for your datepicker but the attribute name should have this values: Checkin and the other Checkout
<input id=datepicker name="Checkin" />
<input id=datepicker name="Checkout" />

Is there a way to indicate the DateTime object only has Date value?

In the database, there are some field is TimeStamp and some are Date, which means some accept both Date and Time, some accept Date only.
I am trying to write a helper class that can generate the SQL by iternate through the data object's property, and generate proper SQL by the data type of the property.
So say, Table "SomeDateTimeTable" has TimeStamp field "A_TimeStamp" and Date field "A_Date"
Then my data object has two property, "public DateTime A_TimeStamp" and "public DateTime A_Date".
But because C# only has DateTime... My program can't distinguish is the DateTime property indicate the database field is TimeStamp or Date....
I wonder if there is anyway to acheive it?
I only can think of...either make another class that has DateTime parameter to indicate that's a Date type... Or check if the time is 00:00:00...
But both way seems.... not so smart?
It seems to me that this is tied more to the schema than to the specific value; i.e. a column tends to be all dates vs datetimes, not usually individual cells within a column.
With that working assumption (which might not fit your scenario) I might use metadata, for example (but not limited to):
[DateOnly]
public DateTime Foo {get;set;}
...
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property,
AllowMultiple=false,Inherited=true)]
public class DateOnlyAttribute : Attribute {}
Then you can use Attribute.IsDefined(member, typeof(DateOnlyAttribute)) to see if it is set per-member.
You can either make your own type (which should be immutable) or use an attribute.

C#: How to adjust the value the user entered in DataGridView?

I have a databound DataGridView. The data source is a typed data set with a table containing two DateTime columns (BeginTimeStamp and EndTimeStamp). I read and write the data to an SQL Server 2005 database using the typed data set's Update command.
The user must enter a date into each of the two columns, which I enforce using the CellValidating and RowValidating events. However, I also need to make sure that the following two rules apply:
The time value for the BeginDate column must always be 00:00:00
The time value for the EndDate column must always be 23:59:59 (or 11:59:59 pm if you like)
As I do not want the user to enter the 23:59:59 all the time, I'd like to somehow change the user's inputs according to 1. and 2. in my code.
Where and how would I do that?
EDIT
Sorry in case I was unclear. The user may enter any date part, however, the time part is fixed at midnight for the BeginTimeStamp and 23:59:59 for the EndTimeStamp.
Example:
The user enters 2009/01/01 01:00:00pm as BeginTimeStamp. My application should change this to 2009/01/01 00:00:00.
The user enters 2009/01/31 01:00:00pm as EndTimeStamp. My application should change this to 2009/01/31 23:59:59.
I'd just display the DateTime as a Date and add the time behind the scenes.
This could be when the user enters the data or equally it could be when you write the data to the database.
If you choose the former then look at the DataGridView.CellEndEdit event.
See Noam's answer for the code to set the time appropriately.
You can add the following lines to your CellValidating method, after your other validations
DateTime newValue = oldValue.Date;
and
DateTime newValue = oldValue.Date.AddDays(1).AddSeconds(-1);
I'm not sure I follow, the user has to enter a value for the two dates but they are always the same? If I understand correctly, why not set default values in the database , then when you read into the dataset the values will already be there
You can also do the validation inside your property setter:
public DateTime BeginTimeStamp
{
get { return _dateTime; }
set
{
// force the time to whatever you want
_dateTime = value.Date;
}
}

Categories