ASP.NET MVC DisplayFormat for Hour - asp.net-mvc-3

I have the following TimeSpan variable in my Model class:
[DisplayFormat(DataFormatString = "{0:HH-mm}", ApplyFormatInEditMode = true)]
public TimeSpan StartingHour { get; set; }
But when I call it from my View I got the error:
#Html.DisplayFor(modelItem => item.StartingHour)
System.FormatException: Input string was not in a correct format.
The value of the variable is: "18:00:00".
What should be in my DisplayFormat initialization? And please keep "get hour and minute separately from the time" advices to yourself.

Try: DataFormatString = #"{0:hh\-mm}"

Try using:
DataFormatString ="{0:hh\\:mm}"
Thanks.

Related

How to check StartDate < EndDate using UoN.ExpressiveAnnotations

I am using ASP.Net Core Razor pages and have a form on which the end date must be validated on the client side to be later than the start date.
The rest of the form is being validated using UoN.ExpressiveAnnotations.
The dates are defined as follows :
[Display(Name = "Start Date")]
[Required]
public DateTime? StartDate { get; set; }
[Display(Name = "End Date")]
[Required]
public DateTime? EndDate { get; set; }
I have tried using
[RequiredIf("EndDate > StartDate", ErrorMessage = "End must be later than Start")]
with the End Date declaration, but this does not provide the validation necessary.
Am I missing something obvious?
Thanks
DOH. I was missing something obvious.
It should have been
AssertThat
not
RequiredIf

Datetime validation with custom format. Troubles with '.' separator

In my web application (asp.new mvc) I try to make validation for date field.
If I use this
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? PeriodBeginFrom { get; set; }
or this
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yyyy}")]
public DateTime? PeriodBeginFrom { get; set; }
it works. But if I try to separate using '.' separator
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yyyy}")]
public DateTime? PeriodBeginFrom { get; set; }
I have an issue. jQuery validation says that I have to enter date if I try '01.01.2001'.
Here is view
<div class="ElementDiv">
#Html.LabelFor(m => m.Filter.PeriodBeginFrom, new { #class = "Labels" })
#Html.EditorFor(m => m.Filter.PeriodBeginFrom, new { #class = "TextBoxes" })
#Html.ValidationMessageFor(m => m.Filter.PeriodBeginFrom, null, new { #class = "Errors" })
</div>
Why? With all other separators it works. What a problem is with '.' separatior?
Update 1: It seems that validation does not work properly. If I have this attribute at my model
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? PeriodBeginFrom { get; set; }
and we pass DateTime object to model, it will be displayed at textbox with '.' like '12.12.2012' and not like I was waiting '12/12/2012'. Hope you'll give me some ideas.
You're looking at a date component. But remember that time components can include seconds and fractional seconds, with a period inbetween. Since dates and times are frequently combined, I would not be surprised if the period is being treated as a special character.
Update:
I went through the following documentation chain:
DisplayFormatAttribute.ApplyFormatInEditMode Property
DisplayFormatAttribute.DataFormatString Property
Formatting Types
Custom Date and Time Format Strings
I expected to see that the period character has a special meaning in the format string. Instead, the documentation doesn't specifically list the period character. That means it should fall in the categry of "Any other character", and therefore, "The character is copied to the result string unchanged."
However, I don't believe it. Fractional seconds occur in time components like 23:59:59.999, and I suspect there is undocumented special treatment of the period character.
Trouble with a format was not with ASP tags and fields but with unobtrusive validation jquery library. I had to overload unobtrusive JavaScript validation method for date for my date format. More details are here
MVC DateTime validation - UK Date format

format datetime value in C#

i want to format date time value in this format ("yyyy/MM/dd")
below is the code, m using to do so
DateTime? date = DateTime.Now;
DateTime? formattedDate = Convert.ToDateTime(date.Value.ToString("yyyy/MM/dd"));
Console.WriteLine(formattedDate);
Console.ReadLine();
the code work fine but you can see in the above code that i have declared datetime variable as null which in reverse causing the problem
please suggest what i am doing wrong in the above code.
as i am getting the output as this 9/10/2012 12:00:00 AM
Thanks,
Aaman
The problem is that what you call formattedDate is actually yet another instance of a DateTime. There's no notion of format inside the native .NET DateTime structure. You can talk about formatting only when you convert it to a string:
DateTime date = DateTime.Now;
string formattedDate = date.ToString("yyyy/MM/dd"));
Console.WriteLine(formattedDate);
or if you want to use a nullable DateTime:
DateTime? date = DateTime.Now;
string formattedDate = date.Value.ToString("yyyy/MM/dd"));
Console.WriteLine(formattedDate);
But since your question is tagged with asp.net-mvc-3, there are other ways to format values. For example using the [DisplayFormat] attribute on your view model:
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]
public DateTime? Date { get; set; }
and in your view if you want to display the value of your view model:
#Html.DisplayFor(x => x.Date)
or if you want to generate an input field with properly formatted value:
#Html.EditorFor(x => x.Date)

Localize Type Validation in asp.net MVC3 + Razor

I'm using asp.net MVC 3 with razor view engine on my app.
What happens is, for example when my model has a date field and someone writes something that is not a valid date o get a message like "The value 'asd' is not valid for StartDate",
I don't know how to localize this message for example to get it on Portuguese "Data inĂ¡lida".
Can someone help?
You must set the right attributes on your Model class. Like this:
[Date(ErrorMessageResourceName = "RequiredStar", ErrorMessageResourceType = typeof(Properties.Resources))]
[Required(AllowEmptyStrings = false, ErrorMessageResourceName = "RequiredStar", ErrorMessageResourceType = typeof(Properties.Resources))]
[DataType(DataType.DateTime)]
[Display(Name = "Birthday", ResourceType = typeof(Properties.Resources))]
[UIHint("Date")]
public DateTime Birthday { get; set; }

How to show a custom error message for DisplayFormat attribute In Asp.Net MVC?

How to show a custom error message for
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
The code in my model is following,
[Display(Name = "When was that document issued?")]
[DataType(DataType.Date, ErrorMessageResourceName = "DocumentIssueDate_DataType",ErrorMessageResourceType = typeof(CustomErrorMessages))]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
public DateTime? DocumentIssueDate { get; set; }
In my View when I enter 201 in the date textbox I get the following error message. How do I modify the error message below.
The value '201' is not valid for DocumentIssueDate.
After a lot of research I had no luck. The only solution was to use RegularExpression.
Instead of using:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
You can use:
[RegularExpression(#"^(3[01]|[12][0-9]|0[1-9])[-/](1[0-2]|0[1-9])[-/][0-9]{4}$", ErrorMessageResourceName = "Date Not Valid")]
The RegularExpression matches the date format dd/MM/yyyy.
regular still not test yes but you can search and edit if need
Try see answers Here. I think DisplayFormat designed not for validation, that is why you can not use it to check if entered string is valid.

Resources