DisplayFormat stops UK dates displaying in Chrome? - asp.net-core

I have a property of my model as follows:
[DisplayName("Manufactured Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? DateManufactured { get; set; }
In my scaffolded MVC Core view I have this code to display that value:
<input asp-for="LiftingItem.DateManufactured" class="form-control" />
In Chrome, the input shows the text "dd/mm/yyyy" and I get the error:
The specified value "09/02/2006" does not conform to the required
format, "yyyy-MM-dd".
Whereas in IE I get the actual date (e.g. "09/02/2006"). I think that difference is because Chrome is using the HTML 5 Date type input and so requires the format in "yyyy-MM-dd" format to display it?
If I remove the DisplayFormat attribute, sure enough it works in Chrome, but then in IE the date is in the wrong format ("yyyy-MM-dd" instead of "dd/mm/yyyy").
So, it seems like using DisplayFormat with a non "yyyy-MM-dd" format stops the Tag Helpers working? Is there any way to get it working nicely for both HTML 5 compatible and older browsers?
PS: I've also tried applying the formatting in the view instead using asp-format="{0:dd/MM/yyyy} as described in this question, but that also stops it working in Chrome.
Edit: I could also remove [DataType(DataType.Date)], but then I don't get the built in datepicker on HTML5 compatible browsers, which I would definitely like to on mobile devices.

Yes, you can combine the Tag Helpers with the DisplayFormat data annotation.
So, it seems like using DisplayFormat with a non "yyyy-MM-dd" format stops the Tag Helpers working? Is there any way to get it working nicely for both HTML 5 compatible and older browsers?
Just remove [DataType(DataType.Date)] from your model and the DisplayFormat(..) statement will start working as expected.
PS: I've also tried applying the formatting in the view instead using asp-format="{0:dd/MM/yyyy} as described in this question, but that also stops it working in Chrome.
You can combine the DisplayFormat(..) data annotation with the asp-format tag helper if you should require, in a specific view, a different date format than the one specified in your model.
If you have the following property in your model:
[DisplayName("Manufactured Date")]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? DateManufactured { get; set; }
and a view (a Detail view for instance) with the following line of code:
#Html.DisplayFor(model => model.DateManufactured)
Then you application will display dates having a format corresponding to the one specified with the data annotation, i.e. {0:dd/MM/yyyy}. Here's an example: 23/02/2017.
If you add in another view (an Edit view for instance) the following line of code (please note that I've added a 'M' to the month format):
<input asp-for="LiftingItem.DateManufactured" asp-format="{0:dd/MMM/yyyy}" class="form-control" />
Then the asp-format will take precedence over the model's data notation and your application users could type dates like this one: 23/Feb/2017 in the input box.

Related

Replace part of Display Name text while displaying in LabelFor

I have a configuration that is saved in the database. I need to show that configuration in the label name. I am unable to use a replace functionality in the Razor view while fetching label value from Display Name.
Model Code:
[Display(Name = "SomeFooText XYZ typing speed")]
public decimal SomeFooText{ get; set; }
Something like below. I'm unable to find a code that would work for this scenario.
#Html.LabelFor(model => model.SomeFooText).Replace ("XYZ", model.configurationfromDB)
or
(<label asp-for="SomeFooText"></label>).Replace ("XYZ", model.configurationfromDB)
In worst case, I can move the code to c# and hardcode the "SomeFooText XYZ typing speed" and then use string.replace or use javascript to replace but just want to see if there is something that can be used on control directly.

How to sendkeys "time" in time type element?

I need to pass data in time format in "time" type element in "10:00 AM" format.
I am using following code:
public static void setShift()
{
txttime.sendkeys("1030AM");
}
this is not working. what is a correct way to enter such data?
Use Following Code :
It will work it for textbox/text area control
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm a");
Date date = new Date();
txttime.sendkeys(date);
For the HTML input type datetime-local handling it from selenium is not ideal. It is not the most used date time picker, and it is not supported in firefox or safari.
For chrome, the date time format shows the format set in the browser's machine.
If you haven't changed anything, I'm guessing you are getting the format shown in the guru99 tutorial.
If that is the case, then you have missed that they also have provided the solution there.
After entering the date part you need to press tab to input the time part. Which is missing from your given code.
Try this:
First, input the date
WebElement dateBox = driver.findElement(By.xpath("//form//input[#name='bdaytime']"));
dateBox.sendKeys("09252013");
Second, press tab
dateBox.sendKeys(Keys.TAB);
Last, input the time
dateBox.sendKeys("0245PM");
Note:
If your machine has different DateTime formatting then this might not work. You have to check which part of the date time senKeys can actually input then split up that part and use Keys.TAB to press tab. Then input the next part until completion.

How to set string to datetime widget?

The title is self explanatory. I am having difficulties setting a string value to a date time widget.
XCP has a build in function stringToDate
which i use in these examples...
1. stringToDate('5-5-2009')
2. stringToDate('05-05-2009')
3. stringToDate('5/5/2009')
But non of them work. What am I missing here ?
Also i set the value of the widget in the behaviors tab of the date widget.
If you are talking about Date-Time Input widget then you need to use dateToString not stringToDate.
I figured out the issue. It's dependent to the format in which you save your date to string value. If we save the Date-Time Input widget value in this format:
dateToString('12-12-2018', 'm-d-Y')
then doing a :
stringToDate(savedvalue) will work only when the format saved was 'm-d-Y'. It wasn't working before because i saved it as 'd-m-Y'.

Monotouch Dialog Reflection Date element

I am trying to use Monotouch Dialog reflection API to bind to a class.
I have the following in my class:
[Entry("Birth Date")]
public DateTime BirthDate;
When this renders, it renders as a DateTime, which I suppose it expected.
Is there anyway to force this to be a date only
Thanks,
Steve
Should have read the documentation.
You can get a Date only selection by decorating the field with
[Date] instead of [Entry(....

FormBlock Server Control in Ektron

I am working in Ektron 8.6.
I have a FormBlock Server Control in my Template Page,It is having a DefualutFormID of a valid HTML form from workarea.The form in the workarea have got few form fields and their corresponding values.
While the template page is rendering I need to GET those form field values and re-set them with some other values.
In which Page –Cycle event I should do this coding?
I tried this code in Pre-Render Event,but I am unable to GET the value there,but I am able to set a value.
I tried SaveStateComplete event as well,no luck.
String s=FormBlock1.Fields["FirstName"].Value;
If(s=”some text”)
{
// Re-set as some other vale.
FormBlock1.Fields["FirstName"].Value=”Some other value”;
}
In which event I can write this piece of code?
Page_Load works fine for changing the value of a form field. The default behavior is for the Ektron server controls to load their data during Page_Init.
The real problem is how to get the default value. I tried every possible way I could find to get at the data defining an Ektron form (more specifically, a field's default value), and here's what I came up with. I'll admit, this is a bit of a hack, but it works.
var xml = XElement.Parse("<ekForm>" + cmsFormBlock.EkItem.Html + "</ekForm>");
var inputField = xml.Descendants("input").FirstOrDefault(i => i.Attribute("id").Value == "SampleTextField");
string defaultValue = inputField.Attribute("value").Value;
if (defaultValue == "The default value for this field is 42")
{
// do stuff here...
}
My FormBlock server control is defined on the ASPX side, nothing fancy:
<CMS:FormBlock runat="server" ID="cmsFormBlock" DynamicParameter="ekfrm"/>
And, of course, XElement requires the following using statement:
using System.Xml.Linq;
So basically, I wrap the HTML with a single root element so that it becomes valid XML. Ektron is pretty good about requiring content to be XHTML, so this should work. Naturally, this should be tested on a more complicated form before using this in production. I'd also recommend a healthy dose of defensive programming -- null checks, try/catch, etc.
Once it is parsed as XML, you can get the value property of the form field by getting the value attribute. For my sample form that I set up, the following was part of the form's HTML (EkItem.Html):
<input type="text" value="The default value for this field is 42" class="design_textfield" size="24" title="Sample Text Field" ektdesignns_name="SampleTextField" ektdesignns_caption="Sample Text Field" id="SampleTextField" ektdesignns_nodetype="element" name="SampleTextField" />