How to leave a datepicker unfilled (with its value null)? - c#

I am developing a ASP.NET MVC 5 website. I have the Employee model which looks something like this:
public class Employee
{
public EmployeeID {get;set;}
//... other properties such as FirstName, Salary which are not important right now
public DateTime? EndingDate {get; set;} //this is important
}
The corresponding part of the view is like this:
<div class="form-group">
#Html.LabelFor(model => model.EndingDate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
<input name="endingDate" type="date" class="form-control datepicker" />
#Html.ValidationMessageFor(model => model.EndingDate, "", new { #class = "text-danger" })
</div>
</div>
As you can see the EndingDate is nullable because I do not necessarily fill the EndingDate field. If I want to create an employee without filling the EndingDate I get a validation error, which makes sense because on the datepicker the value is exactly the following dd.mm.yyy. This happens on Google Chrome. On Mozilla it is null by default but is behaving like a textbox not like a datepicker. I am using bootstrapper css classes, I didn't wrote a single line of front-end development code; if you accept the language. I tried to add to model the DisplayFormat attribute but no success so far.
Any ideas? Or it is a matter of browsers?

At this point a don't know any method. You can try a text box where you will fill the data and validate it in a dynamic way (using Angular by chance). Think about filing a data from a dumb phone. You enter the digits and it feels the data.

dateTimePicker1.CustomFormat= " ";

Related

Excluding time from TextBoxFor when ViewModel setup as DataType of Date

I am trying to properly display a (nullable) Date-type-only field. For this field, my Model and ViewModel classes are defined as....
[DisplayFormat(DataFormatString = "{0:M/dd/yyyy}")]
[DataType(DataType.Date)]
public DateTime? PeriodEnd { get; set; }
In my details view (based on model), it's showing the date properly (excluding the time element):
#Html.DisplayNameFor(model => model.PeriodEnd)
Problem: In my edit view (based on ViewModel), it's showing the time also, which I'm trying to exclude. Here's how it's defined.
#Html.TextBoxFor(model => model.PeriodEnd, new { #class = "datepicker" })
I also tried....
#Html.TextBoxFor(model => model.PeriodEnd.Value().ToShortDateString(),
new { #class = "datepicker" })
... but that produced an error.
Is there a better way to accomplish this?
Well, first, you're not utilizing the data annotation. Something like DataType.Date doesn't do anything on its own. Using editor templates (Html.EditorFor), it can be utilized to provide an date type input, but if you use Html.TextBoxFor, it's basically ignored.
You can fix that by either 1) using Html.EditorFor instead or 2) explicitly setting the type: #Html.TextBoxFor(m => m.PeriodEnd, new { type = "date", #class = "datepicker" }).
This was the solution:
#Html.TextBoxFor(m => m.PeriodEnd, "{0:M/d/yyyy}", new { #class = "datepicker" })
Thanks to all for your feedback!

Binding lost on #Html.EditorFor when I apply formatting

When using the following I lose the binding on the field
#Html.EditorFor(model => model.Quote.DiscountRate, new { #class = "form-control pull-left " })
And the model field looks like this:
[DisplayFormat(DataFormatString = "{0:P2}",ApplyFormatInEditMode =true)]
public double? DiscountRate { get; set; }
if I remove the DisplayFormat the binding still works.
I also tried the following with the same result:
#Html.TextBoxFor(model => model.Entity.DiscountRate, "{0:P2}", new { #class = "form-control pull-left" })
In both cases if I remove the formatting I get my binding back
What I have discovered is that when you apply a DisplayFormat, it is as the name suggests, for display. If you want to display a decimal value with formatting and make it available for editing, you have to handle the conversion from text back to decimal at post time -- or whenever you need the actual value.

MVC storing date without time

I am trying to save the date the user enters into a database. The problem is that despite only the date showing in the text box the time is also being saved. This is what it looks like in my model.
{11/02/2015 00:00:00}
However this is what I have in my JQuery set up and how I would like it to look when saved to the database, without the time also attached.
$("#datepicker").datepicker({ dateFormat: 'dd/mm/yy' });
I assume the problem lies in my model somewhere so this is how I have it set up at the moment.
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime DateOfBooking { get; set; }
and this is my view,
<div class="form-group">
#Html.LabelFor(model => model.DateOfBooking, new { #class = "control-label col-md-2"})
<div class="col-md-10">
#Html.TextBoxFor(x => x.DateOfBooking, new { #id = "datepicker" })
#Html.ValidationMessageFor(model => model.DateOfBooking)
</div>
</div>
There's no way, using DateTimes, to only store/pass the date to the model. In SQL you can use a Date data type that will only store the Date part of the DateTime object from .Net. However, when you move data into/out of your model you'll always have a time component. The easiest way is to just ignore the time component and specify the display format like you're doing.
You could also use something like Noda Time that has structs for Date only types.

Set a view's textbox (datetime field) to display readonly current datetime

I have a textbox on my view and I'm only looking it to display the current date and time(in readonly..how can this be done?
Currently I have this in the view:
<div class="editor-field">
#Html.EditorFor(model => model.Opened, new { #value = System.DateTime.Now, #readonly = "readonly" })
#Html.ValidationMessageFor(model => model.Opened)
</div>
..And this in the model:
[Required]
public DateTime Opened
{
get;
set;
}
How can this be implemented in MVC?
Thanks,
I recommend you using a Label. I think it's unusual display dates and time at TextBoxes.
Regards.
Well, you can do that, but #Angel Manuel Garcia Car's suggest is what you should be doing logically.
Anyways, here is the code. As long as you are creating a textbox, you should be using TextBoxFor.
#Html.TextBoxFor(model => model.Opened, new { #value = DateTime.Now, #readonly="readonly" })
I don't see any point here.

Kendo TimePickerFor Not Binding to ViewModel When using TimeSpan MVC

I have an application that warrants the need for users to supply a "best fit time of day" for a particular day of week. I am trying to represent that time of day as the timespan object in C# (just as the DateTime.TimeOfDay object does) I don't need the date, but also don't want to use datetime and have the user see an actual date.
Right now i have this in my model:
[Required, Display(Name = "Start")]
public TimeSpan StartTime { get; set; }
[Required, Display(Name = "End")]
public TimeSpan EndTime { get; set; }
I have this for part of my view:
<div class="form-group col-xs-6 col-lg-4">
#Html.LabelFor(m => m.StartTime, new { #class = "control-label" })
#(Html.Kendo().TimePickerFor(m => m.StartTime).Interval(15).Culture("en-Us").HtmlAttributes(new { #class = "form-control" }))
#Html.ValidationMessageFor(m => m.StartTime)
</div>
<div class="clearfix"></div>
<div class="form-group col-xs-6 col-lg-4">
#Html.LabelFor(m => m.EndTime, new { #class = "control-label" })
#(Html.Kendo().TimePickerFor(m => m.EndTime).Interval(15).Culture("en-Us").HtmlAttributes(new { #class = "form-control" }))
#Html.ValidationMessageFor(m=>m.EndTime)
</div>
<div class="clearfix"></div>
I keep getting this model error using the MVC ModelState:
ErrorMessage = "The value '1:00 PM' is not valid for Start."
I'm not sure what i'm doing wrong, intellesense says that the kendo widget supports a timespan for it's input, but why isn't this actually binding to the view?
i have seen this article: Kendo Timepickerfor formatting not working
It's not quite what i'm looking for, because i feel like there should be a more simple way to represent a time of the day that is unrelated to the actual date...
Any help is greatly appreciated!
Thanks!
Using DateTime seemed to work like you had said #Saranga
Not too thrilled it stores the entire date in the database, as it's not immediately clear then that this is only relevant to the time only, but it works.
Thanks for your help!

Categories

Resources