MVC4 date input locale - asp.net-mvc-4

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.

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.

why does a nullable datetime trying to validate the date, when it is empty?

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?

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.

Getting the message from jquery validation when using UnobtrusiveJavaScript

Hey there StackOverflow enthusiasts.
I am attempting to update an old site of mine to the newest edition of Asp.net MVC4. It was previously on MVC2, and on MVC2 it allowed me to pretty much separate my jquery and client side stuff from any of the backend stuff. But I guess with MVC4 they want you to have a lot of your validation stuff tied directly to your models and so on.
But I have a client side jquery validation script that was working pretty well before, and I was wondering, how can I get it to work again. Specifically, I had a field that would not be validated if the user entered in more than 4000 characters. Otherwise it would not be required. Here is the client side code that worked before in MVC2....
Comment: {
required: function (element) {
return $(element).val().length > 4000;
},
maxlength: 4000
}
and the message that would be displayed if validation was not passed...
messages: {
...
Comment: 'Why dont you stop attempting to put in more than 4000 characters? Thanks...'
}
How can I do that with MVC four? To get anything to display in another field I noticed I needed to put a required over the field in my model...
[Required(ErrorMessage = "Please select an Admit Date")]
public DateTime? AdmitDate { get; set; }
Is there a way to write a requirement function so that it is only required under certain circumstances? I would prefer to keep it client side to keep my concerns separate if you know what I mean. Any help would be appreciated.
You can use [Required] and [StringLength] to constrain the input on your Comment.
[Required(ErrorMessage = "Please fill in a comment")]
[StringLength(4000, ErrorMessage = "Why dont you stop attempting to put in more than 4000 characters? Thanks...")]
public string Comment { get; set; }
Conditional required constraints are not covered by default data annotations. The default way of handling 'complex' validations is by implementing the IValidatableObject interface on your model.
However, since you are looking for a client-side solution, we can look at other frameworks that may solve this problem for you. For instance, MVC Foolproof Validation supports a [RequiredIf] attribute that also works client-side.