I searched for an answer to my question, which is about how to make a date only input equipped with custom date picker in Razor ASP MVC.
The core of the problem is that I also implemented localisation to my app.
Firstly I added attribute to to my parameter in model:
[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)]
This is causing troubles when the language of the application is switched, because the format is valid for only one language.
Second approach was to add another attribute to model class:
[DataType(DataType.Date)]
With this, I achieved that Chrome/Opera/Vivaldi offered a browser integrated date picker.
<input type="date"/>
However, this did not work correctly, because it used browser language and not user selected language for the application. So server expects dates in different format as it might be posted to. So the validation fails. On the other hand, the good old IE showed simple input field which had date of correct format inside.
Third approach was converting DateTime object to local string in HTML helper with ToShortDateString() function:
#Html.EditorFor(model => model.ConstructionStart_PLANNED.ToShortDateString(), new { htmlAttributes = new { #class = "form-control" } })
The results as an exception.
Any help would be greatly appreciated.
Related
I am working on an application whose target will be the greek market. My problem is that I want the date input fields to have the greek date format (which is dd-mm-yyyy) and the calendar to be in greek regardless of the browser settings. I suspect that there is some way to achieve this using the Locale attribute of either the Session, or the input field, in order to avoid using some 3rd party plugin (like the jQuery datepicker). Am I correct in thinking so? And if yes, what is the proper way to do this?
What I tried to do was setLocale("el") when initiating the input field and when creating a new session, both of which did not work out.
Override Application#newSession:
#Override
public Session newSession(Request request, Response response)
{
final Session session = super.newSession(request, response);
session.setLocale(yourFixedLocale);
return session;
}
In my application, a user can specify a date in a form, via a datepicker. Doing so, the date has this format : "2018-05-16T12:45:30Z".
Then, I want to store it in a MySql database, in a TIMESTAMP column.
Later, the user can edit his data. Consequently, the datepicker has to be initialized with the date coming from the server, previously saved.
To manage this, I created an accessor and a mutator :
public function setDateNameInputAttribute($value)
{
$this->attributes['date_name_input'] = Carbon::createFromFormat('Y-m-d\TH:i:s\Z', $value);
}
public function getDateNameInputAttribute($value)
{
return Carbon::parse($value)->format('Y-m-d\TH:i:s\Z');
}
This code works fine : my front-end reads UTC (Zulu) dates and I can insert timestamps in my database.
However, it's not perfect.
Let's say I need for whatever reason to add one hour to a stored date
$myObject = MyClass::find(1);
$theDate = $myObject->dateNameInput;
Now $theDate is a "T Z format" string, because of the accessor. I could recreate a Carbon object to do my addition, but I think this Carbon -> string -> Carbon transition would be ugly. How can I make a nice operation ?
If my applications contains a lot of input dates, with many different model names, is there a way to generalize my accessor and my mutator ?
Actually, is my first approach good ?
Thanks for reading !
I have been facing an issue with Web API 2.2 with Web API OData controller. I am doing a PoC in which I need to display the JSON output from my service with different column names than that of corresponding Model props (DB table columns).
(For ex: 'CompanyName' from Customer table should appear as 'cName' in JSON output of my service.)
I am using DB first approach with Northwind database, created a Model with ADO.NET EF and created a controller using OData EF. (all default no code changes so far)
Now, I have tried to get different names using
1) Data Contract and Data Member -> specifying directly on Model class (yes, auto generated one)
2) JsonProperty -> specifying directly on Model class (yes, auto generated one)
3) DTOs [it works but I don't want use DTOs]
Unfortunately, first 2 approaches are not working for me (not sure what I'm missing here) and DTOs I'm trying to avoid.
I'm stuck on this all my day today, appreciate if you can point me to a right approach.
Note: Instead of OData controller if I use regular Web API controller, all works.
I realize this is old, and I'm not sure which version of OData you are using but the simple answer is, you have to specify all of this information in the model builder.
Here's an example.
var builder = new ODataConventionModelBuilder();
var entity = builder.EntitySet<Model>("models").EntityType;
entity.Name = "model";
entity.Property(p => p.Id).Name = "id";
entity.Property(p => p.Name).Name = "name";
entity.Property(p => p.Description).Name = "description";
Good luck!
I am not sure if I am the only one having this issue on Chrome Browser but it is really annoying that I cannot fix it. I have done quite a bit of search here but nothing has helped.
First of all here is my ViewModel Data Annotation
[Required]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd hh:mm}")]
[DisplayName("Start Date")]
public DateTime? StartDate { get; set; }
On the view I am using datetimepicker bootstrap extension
When I am on the process of Creating the date I just select the date from the calander then I select the time as well. On the creating process there is no issue on Chrome and EI10. However, once the date has been created I go to edit mode to change the date. The date is displayed in dd/MM/yyy hh:mm format(example 18/03/2014 10:00) instead of yyyy-MM-dd hh:mm (2014-03-18 10:00). Without changing the date I click Update button on the page I get the validation error for the date fields.(See the screenshot) This is only in chrome browser. in EI10 everthing works fine. This is the first time that I am experiencing that something works on IE but not in Chrome :)
I have also set the datetimepicker date format to
$('.startdate').datetimepicker({ format: 'yyyy-MM-dd hh:mm' });
But still I cannot change the display format of the field to yyyy-MM-dd hh:mm. The last thing that comes in my mind that I will have to convert the format in the controler before it gets passed the view? But I thought the ViewModel annotations is actually should be doing that for me or I am wrong ?
I found a line of code that people are suggesting for the issue
jQuery.validator.methods["date"] = function (value, element) { return true; }
I still couldn't get it work
I'm using Spring Roo on top of MySQL. I pull dates out via the Roo-generated JSON methods, make changes to that data in a browser form, and save it back via the Roo-generated JSON methods.
The date format I'm getting out is yyyy-MM-dd, standard MySQL date format. I'm using a calendaring widget on the browser to ensure the date I'm submitting is the same format.
Unfortunately my data doesn't go right through the ...FromJson() method, failing with the error:
Parsing date 2007-12-12 was not recognized as a date format
I presume that the problem is that it's coming out as a string, but JPA feels like it needs to generate a Date object to update.
I'll happily show my code about this, but it's nothing Roo didn't build for me.
It occurs to me that there's something it's referring to when it says "recognized as a date format". Is there somewhere I can change what date formats it knows?
EDIT: With #nowaq's help, here's the ultimate answer:
public static Lease fromJsonToLease(String json) {
return new JSONDeserializer<Lease>()
.use(null, Lease.class)
.use(Date.class, new DateFormatter("yyyy-MM-dd"))
.deserialize(json);
}
That way JSONDeserializer knows what class it's dealing with AND builds a formatter for all the dates in that class. Wicked!
Your question is very related to this one: Spring-roo REST JSON controllers mangle date fields Take a look and make sure you're using correct DateTrasformer(s) with your JSON deserializers. E.g.
new JSONDeserializer()
.use(Date.class, new DateTransformer("yyyy-MM-dd") )
.deserialize( people );