elmbrook
asked on
MVC4 - Convert Blank Date TextBoxFor Field
Is there a way to format null dates (01-01-1900) in MVC4
Currently, I have:
But to do that for all date fields makes a lot of work.
Is there a way around this.
If so, can someone please provide a detailed example.
thanks.
Currently, I have:
@if (Model.DOB == Convert.ToDateTime("01-01-1900"))
{
@Html.TextBoxFor(model => model.DOB, new { @Value = "", @class = "textbox150" })
}
else
{
@Html.TextBoxFor(model => model.DOB, new { @Value = Model.DOB.ToShortDateString(), @class = "textbox150" })
}
But to do that for all date fields makes a lot of work.
Is there a way around this.
If so, can someone please provide a detailed example.
thanks.
ASKER
How would that work?
I get an error message when I put in
@Html.TextBoxFor(model => model.DOB.HasValue ? model.DOB.ToString("yyyy-M M-dd") : "", new { @class = "textbox150" })
'System.DateTime' does not contain a definition for 'HasValue' and no extension method 'HasValue' accepting a first argument of type 'System.DateTime' could be found (are you missing a using directive or an assembly reference?)
I get an error message when I put in
@Html.TextBoxFor(model => model.DOB.HasValue ? model.DOB.ToString("yyyy-M
'System.DateTime' does not contain a definition for 'HasValue' and no extension method 'HasValue' accepting a first argument of type 'System.DateTime' could be found (are you missing a using directive or an assembly reference?)
How is DOB defined in the model?
You should be able to add a ? to the DateTime declaration:
Example:
When working with nullable value types (like int or DateTime), it can introduce other issues, but you should be able to check if the property has a value (model.DOB.HasValue).
You should be able to add a ? to the DateTime declaration:
Example:
public DateTime? DOB { get; set; }
When working with nullable value types (like int or DateTime), it can introduce other issues, but you should be able to check if the property has a value (model.DOB.HasValue).
ASKER
I have put the code in the View, not the controller.
This is how DOB is defined.
This is how DOB is defined.
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.DateTime DOB
{
get
{
return _DOB;
}
set
{
OnDOBChanging(value);
ReportPropertyChanging("DOB");
_DOB = StructuralObject.SetValidValue(value);
ReportPropertyChanged("DOB");
OnDOBChanged();
}
}
You need to change it to a nullable type:
public global::System.DateTime
becomes
public global::System.DateTime?
public global::System.DateTime
becomes
public global::System.DateTime?
ASKER
It won't allow me to do that.
I am in my model designer and when I add a question mark after it, it gives me errors (see screen shot attached).
24-10-2014-9-39-48-a-m-.jpg
I am in my model designer and when I add a question mark after it, it gives me errors (see screen shot attached).
24-10-2014-9-39-48-a-m-.jpg
If you are using the Entity Framework designer, then I would have thought that it generated the property type as DateTime?. If it is not Entity Framework, then what do you use to design the data model?
ASKER
I am using entity framework. My database table fields are all set to not null.
There are no null dates in the database, all I am trying to handle is 01-01-1900 (blank dates).
That was the only way that I could think off on handling blank dates but that means for every date I have to write that code.
I was hoping to find a better solution of handling blank dates.
There are no null dates in the database, all I am trying to handle is 01-01-1900 (blank dates).
That was the only way that I could think off on handling blank dates but that means for every date I have to write that code.
I was hoping to find a better solution of handling blank dates.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Bob - The solutions provided are just that, solutions.
I think my original solution is the way to go, it just means I have to process each date manually.
thank you for your time and input.
I think my original solution is the way to go, it just means I have to process each date manually.
thank you for your time and input.
var displayDate = model.BirthDate.HasValue ? model.BirthDate.Value.ToSt