why does a nullable datetime trying to validate the date, when it is empty? - asp.net-mvc-4

I am building an MVC 4 application. I have the following property
[Display(Name = "ExpiryDateLabel", ResourceType = typeof(Resources.Manage.PrereqDetail))]
public DateTime? ExpiryDate { get; set; }
in my View when i post it back with an empty value it says
The field Expiry Date: must be a date
why is that happening?

Looks like you are doing localization on ExpiryDate property and if you are using jQuery validation, you might wanna check following post explaining the solution to same problem.
How to fix regional settings for jQuery datepicker so it works in Firefox and IE7?

Related

ASP.NET MVC Core date only format is not working using DataAnnotation

Following viewmodel used in a view is supposed to display a StartDate as, say 9/30/2015. But it is displaying as 9/30/2015 12:00:00 AM. How can I make it display without time while using DataAnnotaion? I know I can use #Model.StartDate.ToString("MM/dd/yyy") inside view to display date only. But that would mean you have to do it in every view that is using the following ViewModel:
ViewModel:
...
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime StartDate { get; set; }
...
UPDATE
The corresponding model class of the above ViewModel already has the following DataAnnotation that correctly creates the data type in SQL Server table as Date; and when you run a query on the corresponding table in SSMS it correctly displays the StartDate column's data with dates only, say, 9/30/2015 etc.)
Model
...
[DataType(DataType.Date)]
public DateTime StartDate { get; set; }
...
StartDate in the sql db is in fact Date only. Moreover, if I run a query on SSMS it correctly returns date only.
There are two solutions to your problem. As per your comments, you are currently using #Model.StartDate to display a date.
You can either do this:
#Model.StartDate.ToString("d")
Or, you can use a model based approach in your ViewModel and do this:
[DataType(DataType.Date)]
public DateTime StartDate {get;set;}
Then, in your view use:
#Html.DisplayFor(m => m.StartDate)

MVC4 Code First Database type "Date"

I need a database column to be a date data type in a MS SQL Server. It's a requirement for the project, it cannot be datetime.
In my model I have:
[DataType(DataType.Date)]
public DateTime? RequestedCompletionDate { get; set; }
This still creates the column as a nullable datetime. Is there any way to make it date code first?
After a lot of research I finally figured out how to do it.
In my model rather than using the DataType attribute I used the column attribute. My model now looks like this:
[Column(TypeName="date")]
public DateTime? RequestedCompletionDate { get; set; }
If anyone is trying to do something similar, although I used TypeName="date", you should be able to set TypeName to anything you want as long as it is a valid SQL Server Data Type.

ASP.Net MVC validation message localization

I have an application that I use for localization. I have added the necessary resource files for the different languages and I get the localized string for labels based on the current culture. So that's fine. I have also added the DataAnnotations for Required like below :
[Display(Name = "Date_Of_Birth", ResourceType = typeof(i18n.Resources.Generic))]
[Required(ErrorMessageResourceType = (typeof(i18n.Resources.Generic)), ErrorMessageResourceName = "InvalidDate")]
[DataType(DataType.Date)]
public DateTime Date_Of_Birth { get; set; }
I have localized the error message and have the corresponding values stored against "InvalidDate". However, I do not get the correct full error message. I get the default "required" message which is a mix of English and the current culture.For example, I get, The field XXXX is required where XXXX is the name of the field in the localized format.
Why is the localized error message not being picked up from the correct localized files?
Thanks in advance for any help.

Required attribute in Data Annotations

I'm using MVC 4 and Entity Framework 5 to build my website. To validate data in client side, I use Data Annotations. Here is my property in View Model:
public int Salary { get; set; }
As you can see, I don't put any annotation there. Also, in my database, the Salary column was marked as Allow null.
My problem is whenever I submit my form, the ModelState is invalid because of this property. It thinks that this property is required and display error on client side.
Do you know what causes the problem? Please help me. Thanks a lot.
The issue you are having is because the default constructor initializes the property to 0. If you want to allow nulls do this:public int? Salary { get; set; }

MVC4 date input locale

I want a simple date field input in MVC4, and the date format accepted should be based on a locale that I define (en-GB).
I don't want to use jQuery date picker or anything like that. I just want to annotate my DateTime? field such that it accepts dates in an en-GB date format, and displays the error message of my choice from a resource file.
I have read a number of posts here and have tried their suggestions without success
I have annotated my model property as follows
[Display(Name = "DateOfBirthLabel", ResourceType = typeof(ApplicantResources))]
[DataType(DataType.Date,
ErrorMessageResourceName = "DateOfBirthValidation",
ErrorMessageResourceType = typeof(ApplicantResources)),
DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? DateOfBirth { get; set; }
And it
Doesn't display the error message that I want it to display
Doesn't use the date format that I want it to (it accepts all American date formats)
I have also tried editing the web config to ensure the locale is set for the UI
<system.web>
<globalization culture="en-GB" uiCulture="en-GB"/>
</system.web>
I think this is a fairly basic requirement but I can't work out how to do it with MVC4. It should be trivial.
After a lot o research I found the right answer. Just take a look at this link.
So just get rid of all you added on your web.config and the annotations in your model.